[英]How do I get these two buttons on the bottom of my program
Write a program that displays two buttons labeled “Green” and “Orange”. 编写一个程序,显示两个标记为“绿色”和“橙色”的按钮。
If the user clicks on the green button, the background of the window changes to green. 如果用户单击绿色按钮,则窗口的背景将变为绿色。 If the user clicks on the orange button, the background of the window changes to Orange. 如果用户单击橙色按钮,则窗口的背景将变为橙色。
Create a JFrame
for this GUI. 为此GUI创建一个JFrame
。 The GUI employs the default layout manager. GUI使用默认的布局管理器。 A JPanel
is needed. 需要一个JPanel
。
Place the two buttons inside the panel and add the panel to the south region of the border layout. 将两个按钮放在面板内,然后将面板添加到边框布局的南部区域。
Notice the text in the title bar. 注意标题栏中的文本。 The green button should have white text and a green background. 绿色按钮应具有白色文本和绿色背景。 The orange button should have black text with an orange background. 橙色按钮应该具有橙色背景的黑色文本。
Below is what I have so far, it doesn't seem to work. 以下是我到目前为止的内容,它似乎没有用。
public class LabAssign91 extends JFrame implements ActionListener{
private JPanel loc1Panel;
private JButton greenButton, orangeButton;
public LabAssign91()
{
super("Colored Buttons");
setLayout(new GridLayout(2, 2));
setSize(300,250);
setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(loc1Panel);
loc1Panel = new JPanel();
add(loc1Panel, BorderLayout.SOUTH);
greenButton = new JButton("Green");
greenButton.addActionListener(this);
loc1Panel.add(greenButton, BorderLayout.WEST);
greenButton.setBackground(Color.green);;
orangeButton = new JButton("Orange");
orangeButton.addActionListener(this);
loc1Panel.add(orangeButton, BorderLayout.EAST);
orangeButton.setBackground(Color.orange);
}
public static void main(String[] args) {
LabAssign91 app = new LabAssign91();
}
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
} }
I have used BorderLayout
for the JFrame
and FlowLayout
for the ButtonPanel
. 我已经将BorderLayout
用于JFrame
并将FlowLayout
用于ButtonPanel
。 ButtonPanel
is the bottom panel of the frame. ButtonPanel
是框架的底部面板。
frame = new JFrame();
frame.setLayout(new BorderLayout());
topPanel = new JPanel();
topPanel.add(new JLabel("Top Panel"));
middlepanel = new JPanel();
middlepanel.add(new JLabel("Middle Panel"));
bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
bottomPanel.add(new JButton("Orange"));
bottomPanel.add(new JButton("Green"));
frame.add(topPanel, BorderLayout.NORTH);
frame.add(middlepanel, BorderLayout.CENTER);
frame.add(bottomPanel, BorderLayout.SOUTH);
The default layout for a JFrame is BorderLayout which has a SOUTH constraint. JFrame的默认布局是BorderLayout,它具有SOUTH约束。 So there is no need for this statement. 因此,不需要此声明。
//setLayout(new GridLayout(2, 2));
The default layout for a JPanel is a FlowLayout. JPanel的默认布局是FlowLayout。 So the following statements do nothing: 因此,以下语句不起作用:
loc1Panel.add(greenButton, BorderLayout.WEST);
loc1Panel.add(orangeButton, BorderLayout.EAST);
Read the section from the Swing tutorial on Using Layout Managers . 阅读Swing教程中有关使用布局管理器的部分 。 There is a section on using a BorderLayout and on using a FlowLayout. 关于使用BorderLayout和使用FlowLayout的部分。 I don't know if you are supposed to use just panels with a BorderLayout or panels with a combination of BorderLayout and FlowLayout. 我不知道您是否应该仅使用具有BorderLayout的面板或具有BorderLayout和FlowLayout组合的面板。 I'll let you fix the code to meet your requirement. 我将让您修复代码以满足您的要求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.