简体   繁体   English

如何在程序底部找到这两个按钮

[英]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.

相关问题 如何获得我的百分比以在Dice程序中工作? - How do I get my percent to work in my Dice program? 如何使我的程序执行随机方法? - How do I get my program to do random methods? 我正在为测验程序使用JTabbedPane,不确定如何获取按钮以显示答案 - I'm using JTabbedPane for my quiz program, not sure how to get my buttons to display the answers 如何获得IzPack将程序添加到Windows注册表? - How do I get IzPack to add my program to the Windows Registry? 如何获取我的Java程序以输出答案? - How do I get my java program to output the answer? 我如何让我的程序指出错误并在输入&lt;1时退出 - How do I get my program to state an error and exit if input is < 1 我如何布置我的按钮,以便它们从左上方到右下方(先填充第一行,然后填充第二行) - How do i lay out my buttons so they will go from top left to bottom right (filling first row then second) 如何让对角线从 asterik 框的左下角开始打印? - how do I get my diagonal line to start printing from the bottom left of my asterik box? 如何使TextView到达底部的中心? - How do I get a TextView to the center of the bottom? 我如何到达AST表达式的底部 - How do I get to the bottom of an AST Expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM