java 如何在程序底部获得这两个按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16270935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get these two buttons on the bottom of my program
提问by user2288575
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. The GUI employs the default layout manager. A JPanel
is needed.
JFrame
为这个 GUI创建一个。GUI 使用默认布局管理器。一个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.");
}
}
}
回答by Amarnath
I have used BorderLayout
for the JFrame
and FlowLayout
for the ButtonPanel
. ButtonPanel
is the bottom panel of the frame.
我已经使用BorderLayout
了JFrame
和FlowLayout
为ButtonPanel
。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);
回答by camickr
The default layout for a JFrame is BorderLayout which has a SOUTH constraint. So there is no need for this statement.
JFrame 的默认布局是 BorderLayout,它具有 SOUTH 约束。所以这个说法是没有必要的。
//setLayout(new GridLayout(2, 2));
The default layout for a JPanel is a FlowLayout. So the following statements do nothing:
JPanel 的默认布局是 FlowLayout。所以下面的语句什么都不做:
loc1Panel.add(greenButton, BorderLayout.WEST);
loc1Panel.add(orangeButton, BorderLayout.EAST);
Read the section from the Swing tutorial on Using Layout Managers. There is a section on using a BorderLayout and on using a 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. I'll let you fix the code to meet your requirement.
阅读 Swing 教程中有关使用布局管理器的部分。有一个关于使用 BorderLayout 和使用 FlowLayout 的部分。我不知道你是应该只使用带 BorderLayout 的面板还是结合使用 BorderLayout 和 FlowLayout 的面板。我会让你修复代码以满足你的要求。