如何使用2个JPanel对象对齐JButton以形成基本的java GUI
我正在尝试重新创建以下非常基本的GUI,如图所示: 我的输出如下:如何使用2个JPanel对象对齐JButton以形成基本的java GUI,java,swing,user-interface,awt,layout-manager,Java,Swing,User Interface,Awt,Layout Manager,我正在尝试重新创建以下非常基本的GUI,如图所示: 我的输出如下: import java.util.*; import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; public class GUI extends JFrame implements ActionListener, WindowListener { private JButton circleB
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class GUI extends JFrame implements ActionListener, WindowListener
{
private JButton circleButton;
private JButton rectangleButton;
private JButton redButton;
private JButton greenButton;
private JButton blueButton;
private JButton exitButton;
private JTextField textField1;
private JLabel label1;
private JPanel contentPane;
private JPanel colorPane;
private JPanel shapePane;
private JFrame contentFrame;
private int count;
public GUI (String title)
{
super(title);
//setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setBounds(100, 100, 945, 580);
//contentFrame = new JFrame();
//contentPane = new JPanel();
//contentFrame.add(contentPane);
//contentPane.setBorder(new LineBorder(new Color(50, 5, 232), 4, true));
//setContentPane(contentPane);
//contentPane.setLayout(null);
colorPane = new JPanel();
//colorPane.setBorder(new LineBorder(new Color(34, 174, 82), 1, true));
colorPane.setBounds(10, 32, 515, 125);
//contentPane.add(colorPane);
//colorPane.setLayout(null);
shapePane = new JPanel();
shapePane.setBounds(10, 165, 515, 315);
//shapePane.setBorder(new LineBorder(new Color(34, 174, 82), 1, true));
//contentPane.add(shapePane);
//shapePane.setLayout(null);
circleButton = new JButton("Circle");
circleButton.setHorizontalAlignment(SwingConstants.LEFT);
rectangleButton = new JButton("Rectangle");
rectangleButton.setHorizontalAlignment(SwingConstants.LEFT);
greenButton = new JButton("Green");
redButton = new JButton("Red");
blueButton = new JButton("Blue");
exitButton = new JButton("Exit");
textField1 = new JTextField(20);
label1 = new JLabel("current time here");
colorPane.add(redButton, BorderLayout.CENTER);
colorPane.add(greenButton, BorderLayout.CENTER);
colorPane.add(blueButton, BorderLayout.CENTER);
shapePane.add(rectangleButton, BorderLayout.SOUTH);
shapePane.add(circleButton, BorderLayout.SOUTH);
shapePane.add(exitButton, BorderLayout.SOUTH);
getContentPane().add(textField1, BorderLayout.EAST);
getContentPane().add(label1, BorderLayout.WEST);
getContentPane().add(colorPane, BorderLayout.CENTER);
//contentFrame.add(colorPane);
getContentPane().add(shapePane, BorderLayout.CENTER);
//contentFrame.add(shapePane);
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
我很难格式化JButtons。首先,无论我做什么,我都无法在第一个面板下方显示第二个面板,即“shapePane”和“colorPane”。其次,我无法正确地将按钮的y轴变大,以使它们看起来更胖。此外,顶部面板“shapePane”似乎随着窗口大小的调整而动态移动,而第二个“colorPane”则保持在固定位置,不管窗口大小如何
如果有人能提供一些帮助,我将不胜感激
到目前为止,我的代码如下:
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class GUI extends JFrame implements ActionListener, WindowListener
{
private JButton circleButton;
private JButton rectangleButton;
private JButton redButton;
private JButton greenButton;
private JButton blueButton;
private JButton exitButton;
private JTextField textField1;
private JLabel label1;
private JPanel contentPane;
private JPanel colorPane;
private JPanel shapePane;
private JFrame contentFrame;
private int count;
public GUI (String title)
{
super(title);
//setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setBounds(100, 100, 945, 580);
//contentFrame = new JFrame();
//contentPane = new JPanel();
//contentFrame.add(contentPane);
//contentPane.setBorder(new LineBorder(new Color(50, 5, 232), 4, true));
//setContentPane(contentPane);
//contentPane.setLayout(null);
colorPane = new JPanel();
//colorPane.setBorder(new LineBorder(new Color(34, 174, 82), 1, true));
colorPane.setBounds(10, 32, 515, 125);
//contentPane.add(colorPane);
//colorPane.setLayout(null);
shapePane = new JPanel();
shapePane.setBounds(10, 165, 515, 315);
//shapePane.setBorder(new LineBorder(new Color(34, 174, 82), 1, true));
//contentPane.add(shapePane);
//shapePane.setLayout(null);
circleButton = new JButton("Circle");
circleButton.setHorizontalAlignment(SwingConstants.LEFT);
rectangleButton = new JButton("Rectangle");
rectangleButton.setHorizontalAlignment(SwingConstants.LEFT);
greenButton = new JButton("Green");
redButton = new JButton("Red");
blueButton = new JButton("Blue");
exitButton = new JButton("Exit");
textField1 = new JTextField(20);
label1 = new JLabel("current time here");
colorPane.add(redButton, BorderLayout.CENTER);
colorPane.add(greenButton, BorderLayout.CENTER);
colorPane.add(blueButton, BorderLayout.CENTER);
shapePane.add(rectangleButton, BorderLayout.SOUTH);
shapePane.add(circleButton, BorderLayout.SOUTH);
shapePane.add(exitButton, BorderLayout.SOUTH);
getContentPane().add(textField1, BorderLayout.EAST);
getContentPane().add(label1, BorderLayout.WEST);
getContentPane().add(colorPane, BorderLayout.CENTER);
//contentFrame.add(colorPane);
getContentPane().add(shapePane, BorderLayout.CENTER);
//contentFrame.add(shapePane);
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
以下内容应该可以帮助您开始:
设置标签的垂直和水平位置,使其显示在左下角 以及所需的宽度。对于更多的布局灵活性,考虑在<<代码> jPoCTs/COD> >中修改它:
label1 = new JLabel("current time here");
label1.setVerticalAlignment(SwingConstants.BOTTOM);
label1.setHorizontalAlignment(SwingConstants.LEFT);
label1.setPreferredSize(new Dimension(200, 0)); //height is set by the layout manger
getContentPane().add(label1, BorderLayout.WEST);
为按钮窗格使用网格布局:
colorPane = new JPanel();
colorPane.setLayout(new GridLayout(2, 3));
初始化按钮并将其逐个添加到网格窗格:
redButton = makeButton("Red");
colorPane.add(redButton);
其中makeButton
是一种避免重复代码的方法:
private JButton makeButton(String text) {
JButton b = new JButton(text);
b.setHorizontalAlignment(SwingConstants.LEFT);
b.addActionListener(this);
b.setPreferredSize(new Dimension(125, 55)); //set preferred and let Layout manager do its work
return b;
}
设置文本区域的列数。其高度由布局管理器设置:
textArea = new JTextArea(0,20);
getContentPane().add(textArea, BorderLayout.EAST);
总而言之:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
public class GUI extends JFrame implements ActionListener
{
private final JButton circleButton, rectangleButton, redButton;
private final JButton greenButton, blueButton, exitButton;
private final JTextArea textArea;
private final JLabel label1;
private final JPanel colorPane;
private static final int ROWS = 2, COLS = 3;
public GUI (String title)
{
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set label's vertical and horizontal position so it appears in the bottom left
//and and its desired width
//for more layout flexibility consider warping it in a JFrame
label1 = new JLabel("current time here");
label1.setVerticalAlignment(SwingConstants.BOTTOM);
label1.setHorizontalAlignment(SwingConstants.LEFT);
label1.setPreferredSize(new Dimension(200, 0)); //height is set by the layout manger
getContentPane().add(label1, BorderLayout.WEST);
//use a GridLayout for the buttons pane
colorPane = new JPanel();
colorPane.setLayout(new GridLayout(ROWS, COLS));
getContentPane().add(colorPane, BorderLayout.CENTER);//each BorderLayout position can hold ONE component
redButton = makeButton("Red");
colorPane.add(redButton);
greenButton = makeButton("Green");
colorPane.add(greenButton);
blueButton = makeButton("Blue");
colorPane.add(blueButton);
rectangleButton = makeButton("Rectangle");
colorPane.add(rectangleButton);
circleButton = makeButton("Circle");
colorPane.add(circleButton);
exitButton = makeButton("Exit");
colorPane.add(exitButton);
//set the text area number of columns. Its height is set by the layout manger
textArea = new JTextArea(0,20);
getContentPane().add(textArea, BorderLayout.EAST);
pack();
}
private JButton makeButton(String text) {
JButton b = new JButton(text);
b.setHorizontalAlignment(SwingConstants.LEFT);
b.addActionListener(this);
b.setPreferredSize(new Dimension(125, 55)); //set preferred and let Layout manager do its work
return b;
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(((JButton)e.getSource()).getText()+ " button pressed");
}
public static void main(String[] args) {
new GUI("My Gui").setVisible(true);
}
}
一个简单的增强可以是将所有按钮引用存储在
地图中
:
public class GUI extends JFrame implements ActionListener
{
private Map <String, JButton> buttons; // a map to hold references to all buttons
private final JTextArea textArea;
private final JLabel label1;
private final JPanel colorPane;
private static final int ROWS = 2, COLS = 3;
private static final String[] BUTTON_LABELS = {"Red","Green", "Blue", "Rectangle", "Circle", "Exit"};
public GUI (String title)
{
super(title);
buttons = new HashMap<>();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set label's vertical and horizontal position so it appears in the bottom left
//and and its desired width
//for more layout flexibility consider warping it in a JFrame
label1 = new JLabel("current time here");
label1.setVerticalAlignment(SwingConstants.BOTTOM);
label1.setHorizontalAlignment(SwingConstants.LEFT);
label1.setPreferredSize(new Dimension(200, 0)); //height is set by the layout manger
getContentPane().add(label1, BorderLayout.WEST);
//use a GridLayout for the buttons pane
colorPane = new JPanel();
colorPane.setLayout(new GridLayout(ROWS, COLS));
getContentPane().add(colorPane, BorderLayout.CENTER);//each BorderLayout position can hold ONE component
for(String text : BUTTON_LABELS){
JButton button = makeButton(text);
colorPane.add(button);
buttons.put(text, button);
}
//set the text area number of columns. Its height is set by the layout manger
textArea = new JTextArea(0,20);
getContentPane().add(textArea, BorderLayout.EAST);
pack();
}
private JButton makeButton(String text) {
JButton b = new JButton(text);
b.setHorizontalAlignment(SwingConstants.LEFT);
b.addActionListener(this);
b.setPreferredSize(new Dimension(125, 55)); //set preferred and let Layout manager do its work
return b;
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(((JButton)e.getSource()).getText()+ " button pressed");
}
public static void main(String[] args) {
new GUI("My Gui").setVisible(true);
}
}
公共类GUI扩展JFrame实现ActionListener
{
私有映射按钮;//用于保存对所有按钮的引用的映射
私人最终JTextArea textArea;
私人最终JLabel标签1;
私人最终JPanel彩色窗格;
私有静态final int ROWS=2,COLS=3;
私有静态最终字符串[]按钮标签={“红色”、“绿色”、“蓝色”、“矩形”、“圆形”、“退出”};
公共GUI(字符串标题)
{
超级(标题);
按钮=新HashMap();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置标签的垂直和水平位置,使其显示在左下角
//以及所需的宽度
/对于更多的布局灵活性,考虑在JFrice中修改它。
label1=新的JLabel(“此处的当前时间”);
标签1.设置垂直对齐(旋转恒温器底部);
标签1.设置水平对齐(左摆角);
label1.setPreferredSize(新尺寸(200,0));//高度由布局管理器设置
getContentPane().add(label1,BorderLayout.WEST);
//为按钮窗格使用网格布局
colorPane=newjpanel();
setLayout(新的网格布局(行、列));
getContentPane().add(colorPane,BorderLayout.CENTER);//每个BorderLayout位置可以容纳一个组件
用于(字符串文本:按钮\标签){
JButton button=makeButton(文本);
颜色窗格。添加(按钮);
按钮。放置(文本,按钮);
}
//设置文本区域的列数。其高度由布局管理器设置
textArea=新的JTextArea(0,20);
getContentPane().add(textArea,BorderLayout.EAST);
包装();
}
私有JButton makeButton(字符串文本){
JButton b=新JButton(文本);
b、 设置水平对齐(SwingConstants.左);
b、 addActionListener(此);
b、 setPreferredSize(新维度(125,55));//设置preferred并让布局管理器完成其工作
返回b;
}
@凌驾
已执行的公共无效操作(操作事件e){
System.out.println(((JButton)e.getSource()).getText()+“按下按钮”);
}
公共静态void main(字符串[]args){
新GUI(“我的GUI”).setVisible(true);
}
}
顶部GUI似乎由两个
GridLayout
实例组成。一个用于按钮(并拉伸每个按钮以适合)。然后将该buttonPanel
作为第二个单元格添加到网格布局中,该网格布局为一行,包含dateLabel
、buttonPanel
和textArea
。一般提示:1)setBounds(…)代码>不要调用此方法。布局将忽略结果。2) private JTextField textField1代码>应该是私有JTextArea textArea1
正如其高度所示,它适用于多行文本。但请注意“GUI应如下所示(小偏差即可)。”我认为您当前的GUI应良好,而且,它看起来比提供的图像更好。您想查看GridLayout
谢谢您的帮助。您能告诉我如何为这些按钮添加绘图功能,以便它们在单击时绘制指定的图形吗?我首先尝试实现绘图,然后根据选择的颜色实现形状的颜色。