麻烦简单JPanels

我有一个JFrame,我需要添加面板到它我喜欢的地方,但我不知道该怎么做。我试过所有的布局,但我不能得到我想要的。麻烦简单JPanels

基本上程序化是主框架,我需要在那个框架上的3个其他面板。我需要一个在中间/右边。我需要一个面板在左边,一个在底部,在框架的1/3左右。尝试不同的布局管理器后,我不知道如何去这样做(也对不起,我似乎无法给两个班分开,第一个现在缺少{)

import java.awt.BorderLayout; 

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.Insets;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JPanel;

public class FunProgrammingFrame {

private JFrame frame;

private JMenuBar menuBar;

private JMenu menu;

private JMenuItem menuItem;

private final String M_ITEM_ONE = "File", M_ITEM_TWO = "Project", M_ITEM_THREE = "Help", ITEM_ONE = "New", ITEM_TWO = "Open", ITEM_THREE = "Save", ITEM_FOUR = "Quit";

public FunProgrammingFrame(){

ConsolePanel consolePanel = new ConsolePanel();

frame = new JFrame("Fun Programming");

frame.setLayout(new GridBagLayout());

//frame.add(consolePanel, BorderLayout.SOUTH);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

menuBar = new JMenuBar();

createMenu(M_ITEM_ONE);

addMenuItem(ITEM_ONE);

menuItem.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

}

});

addMenuItem(ITEM_TWO);

menuItem.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

}

});

addMenuItem(ITEM_THREE);

menuItem.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

}

});

menu.addSeparator();

addMenuItem(ITEM_FOUR);

menuItem.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

System.exit(0);

}

});

createMenu(M_ITEM_TWO);

createMenu(M_ITEM_THREE);

//addPanel(consolePanel, 10, 10, GridBagConstraints.SOUTH);

frame.setVisible(true);

}

public void createMenu(String name){

menu = new JMenu(name);

menuBar.add(menu);

frame.setJMenuBar(menuBar);

}

public JMenuItem addMenuItem(String itemName) {

menuItem = new JMenuItem(itemName);

menu.add(menuItem);

return menuItem;

}

public static void main(String[] args){

FunProgrammingFrame fp = new FunProgrammingFrame();

}

}

import java.awt.BorderLayout;

import java.awt.Color;

import javax.swing.JPanel;

public class ConsolePanel extends JPanel{

private JPanel Cpanel;

private final int WIDTH = 100, HEIGHT = 500;

public ConsolePanel(){

Cpanel = new JPanel(new BorderLayout());

Cpanel.setSize(WIDTH, HEIGHT);

Cpanel.setVisible(true);

setBackground(Color.BLACK);

}

}

链接在我需要http://imgur.com/u9NtF7N 的按钮侧面不需要现在

回答:

喜欢的东西...什么

import java.awt.Color; 

import java.awt.Dimension;

import java.awt.EventQueue;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.UIManager;

import javax.swing.UnsupportedLookAndFeelException;

import javax.swing.border.LineBorder;

public class Test {

public static void main(String[] args) {

new Test();

}

public Test() {

EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {

ex.printStackTrace();

}

JFrame frame = new JFrame("Testing");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(new ContentPane());

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

});

}

public class ContentPane extends JPanel {

public ContentPane() {

setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

gbc.gridx = 0;

gbc.gridy = 0;

gbc.fill = GridBagConstraints.BOTH;

gbc.weighty = 0.7;

add(new TestPane(Color.RED), gbc);

gbc.weightx = 1;

gbc.fill = GridBagConstraints.BOTH;

gbc.gridx++;

add(new TestPane(Color.BLUE), gbc);

gbc.gridy = 1;

gbc.gridx = 0;

gbc.weighty = 0.3;

gbc.weightx = 1;

gbc.gridwidth = GridBagConstraints.REMAINDER;

gbc.fill = GridBagConstraints.BOTH;

gbc.anchor = GridBagConstraints.NORTH;

add(new TestPane(Color.GREEN), gbc);

}

}

public class TestPane extends JPanel {

public TestPane(Color color) {

setBorder(new LineBorder(color));

}

@Override

public Dimension getPreferredSize() {

return new Dimension(200, 200);

}

}

}

回答:

您可以让您的主框架成为BorderLayout。

将您ConsolePanel如下:

frame.add(consolePanel, BorderLayout.WEST); 

consolePanel.setPreferredSize(100, 400);

我猜你就可以把它从那里。

回答:

你可以尝试这样的事情:

public class Test { 

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

MBar mBar = new MBar();

TextAreaPanel scrPan = new TextAreaPanel();

BottomPan pan = new BottomPan();

ButtonsPanel buttsCon = new ButtonsPanel();

f.setJMenuBar(mBar);

f.getContentPane().add(buttsCon, BorderLayout.LINE_START);

f.getContentPane().add(scrPan, BorderLayout.CENTER);

f.getContentPane().add(pan, BorderLayout.PAGE_END);

f.pack();

f.setVisible(true);

}

});

}

}

ButtonsPanel.java

public class ButtonsPanel extends JPanel { 

public ButtonsPanel() {

setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

add(Box.createHorizontalStrut(50));

JPanel butts = new JPanel();

butts.setLayout(new BoxLayout(butts, BoxLayout.Y_AXIS));

butts.add(Box.createVerticalStrut(30));

for (int i = 0; i < 5; i++) {

JButton butt = new JButton("Button " + String.valueOf(i));

butts.add(butt);

butts.add(Box.createVerticalStrut(10));

}

butts.setAlignmentY(JPanel.TOP_ALIGNMENT);

butts.add(Box.createVerticalStrut(40));

add(butts);

add(Box.createHorizontalStrut(50));

}

}

TextAreaPanel.java

public class TextAreaPanel extends JScrollPane { 

public TextAreaPanel() {

JTextArea jtxtArea = new JTextArea();

setViewportView(jtxtArea);

}

}

BottomPan.java

public class BottomPan extends JPanel { 

public BottomPan() {

setBackground(Color.black);

setPreferredSize(new Dimension(100, 100));

}

}

MBar.java

public class MBar extends JMenuBar { 

public MBar() {

for (int i = 0; i < 5; i++) {

JMenuItem mi = new JMenuItem("Menu Item " + String.valueOf(i));

add(mi);

}

}

}

以上是 麻烦简单JPanels 的全部内容, 来源链接: utcz.com/qa/261863.html

回到顶部