gpt4 book ai didi

java - 我应该使用什么布局来使它适用于 Java?

转载 作者:行者123 更新时间:2023-11-30 08:53:00 26 4
gpt4 key购买 nike

这就是我想要的最终结果......

Pizza Shop Menu

我正在尝试为此使用 GridBagLayout,但不确定它是否是正确的选择,因为它到处都是。我知道我不应该在一个构造函数中编写我的所有代码,但这是我目前所拥有的...

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.Font;
import java.awt.event.*;
import static java.lang.Math.*;


public class HomeStylePizza extends JFrame{

private static final int WIDTH = 500;
private static final int HEIGHT = 400;

//GUI components
private JLabel lblEachTopping, lblPizzaSize, lblPizzaType, lblWelcome, lblYourOrder;

private JButton btnProcessSelection;

private JCheckBox cbPepproni, cbSausage, cbMushrooms, cbPineapple, cbOnion, cbBellPepper;

private JRadioButton rbSmall, rbMedium, rbLarge, rbThinCrust, rbMediumCrust, rbPan;

private ButtonGroup grp1, grp2;

private JTextArea textArea;

private CalculateButtonHandler cbHandler;


public HomeStylePizza()
{
//Create new labels
lblEachTopping = new JLabel("Each Topping: $1.50");
lblPizzaSize = new JLabel("Pizza Size");
lblPizzaType = new JLabel("Pizza Type");
lblWelcome = new JLabel("Welcome to Home Style Pizza Shop");
lblYourOrder = new JLabel("Your order:");

//Create new buttons
btnProcessSelection = new JButton("Process Selection");
cbHandler = new CalculateButtonHandler();
btnProcessSelection.addActionListener(cbHandler);

//Create new JCheckBoxes
cbPepproni = new JCheckBox("Pepproni");
cbSausage = new JCheckBox("Sausage");
cbMushrooms = new JCheckBox("Mushrooms");
cbPineapple = new JCheckBox("Pineapple");
cbOnion = new JCheckBox("Onion");
cbBellPepper = new JCheckBox("Bell Pepper");

//Create radio buttons
rbSmall = new JRadioButton("Small: $6.50");
rbMedium = new JRadioButton("Medium: $8.50");
rbLarge = new JRadioButton("Large: $10.00");
rbThinCrust = new JRadioButton("Thin Crust");
rbMediumCrust = new JRadioButton("Medium Crust");
rbPan = new JRadioButton("Pan");

//Create new TextArea
textArea = new JTextArea(6, 10);

//Set title
setTitle("Pizza Shop");

//Create new font layout
Font font = new Font("New Times Roman", Font.BOLD, 18);


//get the container
Container pane = getContentPane();

JPanel thePanel = new JPanel();
thePanel.setLayout(new GridBagLayout());

//set the layout
pane.setLayout(new GridBagLayout());

GridBagConstraints gridConstraints = new GridBagConstraints();

gridConstraints.gridx = 1;
gridConstraints.gridy = 1;
gridConstraints.gridwidth = 1;
gridConstraints.gridheight = 1;
gridConstraints.weightx = 50;
gridConstraints.weighty = 100;
gridConstraints.insets = new Insets(5,5,5,5);
gridConstraints.anchor = GridBagConstraints.CENTER;
gridConstraints.fill = GridBagConstraints.BOTH;


//Place components in the pane
thePanel.add(lblWelcome, gridConstraints);
gridConstraints.anchor = GridBagConstraints.FIRST_LINE_START;
gridConstraints.gridwidth = 20;
gridConstraints.gridx = 15;
lblWelcome.setFont(new Font("New Times Roman", Font.BOLD, 18));
lblWelcome.setForeground(Color.RED);

//Create new vertical box and place a titled border around it
Box optionBox1 = Box.createVerticalBox();
optionBox1.setBorder(BorderFactory.createTitledBorder(null, "Toppings", 0, 0, new Font("times new roman", Font.PLAIN, 14), Color.RED));
//add components to optionBox1
optionBox1.add(lblEachTopping);
lblEachTopping.setForeground(Color.RED);
optionBox1.add(cbPepproni);
optionBox1.add(cbSausage);
optionBox1.add(cbMushrooms);
optionBox1.add(cbPineapple);
optionBox1.add(cbOnion);
optionBox1.add(cbBellPepper);
//add optionBox1 to WEST quadrant
thePanel.add(optionBox1, gridConstraints);
gridConstraints.anchor = GridBagConstraints.LINE_START;
gridConstraints.gridwidth = 1;
gridConstraints.gridx = 1;
gridConstraints.gridy = 10;



//Create new vertical box and place a titled border around it
Box optionBox2 = Box.createVerticalBox();
optionBox2.setBorder(BorderFactory.createTitledBorder(null, "Pizza Size", 0, 0, new Font("times new roman", Font.PLAIN, 14), Color.RED));
//Create new ButtonGroup
grp1 = new ButtonGroup();

//add components to optionBox2 and grp1
grp1.add(rbSmall);
grp1.add(rbMedium);
grp1.add(rbLarge);
optionBox2.add(rbSmall);
optionBox2.add(rbMedium);
optionBox2.add(rbLarge);
//add optionBox2 to CENTER
thePanel.add(optionBox2, gridConstraints);
gridConstraints.anchor = GridBagConstraints.CENTER;
gridConstraints.gridx = 3;
gridConstraints.gridy = 10;


Box btnBox = Box.createHorizontalBox();
btnBox.setBorder(BorderFactory.createEmptyBorder());
btnBox.add(btnProcessSelection);
thePanel.add(btnBox);
gridConstraints.anchor = GridBagConstraints.LAST_LINE_END;
gridConstraints.gridwidth = 5;
gridConstraints.gridx = 3;
gridConstraints.gridy = 12;



//Create new vertical box and place a titled border around it
Box optionBox3 = Box.createVerticalBox();
optionBox3.setBorder(BorderFactory.createTitledBorder(null, "Pizza Type", 0, 0, new Font("times new roman", Font.PLAIN, 14), Color.RED));
//Create new ButtonGroup
grp2 = new ButtonGroup();

//add components to optionBox2 and grp1
grp2.add(rbThinCrust);
grp2.add(rbMediumCrust);
grp2.add(rbPan);
optionBox3.add(rbThinCrust);
optionBox3.add(rbMediumCrust);
optionBox3.add(rbPan);
//add optionBox3 to EAST
thePanel.add(optionBox3, gridConstraints);
gridConstraints.anchor = GridBagConstraints.LINE_END;
gridConstraints.gridwidth = 1;
gridConstraints.gridx = 5;
gridConstraints.gridy = 10;


//Create box for lblYourOrder and textArea and
//add them to pane in SOUTH quadrant
Box orderBox = Box.createVerticalBox();
orderBox.setBorder(null);
orderBox.add(lblYourOrder);
orderBox.add(textArea);
thePanel.add(orderBox, gridConstraints);
gridConstraints.anchor = GridBagConstraints.LAST_LINE_START;
gridConstraints.gridwidth = 30;
gridConstraints.gridx = 1;
gridConstraints.gridy = 13;

pane.add(thePanel);


//set window size and display it
setSize(WIDTH, HEIGHT);
setVisible(true);

//Center frame
this.setLocationRelativeTo(null);

this.pack();


setDefaultCloseOperation(EXIT_ON_CLOSE);

}//End of constructor

public static void main(String[] args)
{
HomeStylePizza menuTest = new HomeStylePizza();


}//End of main

private class CalculateButtonHandler implements ActionListener
{

public void actionPerformed(ActionEvent e)
{

}

}




}//End of class

它有点难看,你可以忽略我的大部分评论,我一直在努力解决它。我使用的是不同的布局。

最佳答案

已编辑:我继续创建了 Pizza Shop GUI。

Home Style Pizza

我结合使用了 BorderLayouts 和 GridBagLayouts。

我将代码组合在一起,以便于理解。将代码分组并不意味着将所有 JCheckBox 初始化放在一起。对代码进行分组意味着构成 JPanel 的所有元素都在那里,彼此相邻。这样,您就不必遍历所有代码来查找定义 JRadioButton rbMedium 的行。

我为每个 Swing 组件使用了单独的 GridBagConstraints,以便更轻松地调试 GUI。

我已将 Action 监听器留给您编写代码。

这是 GUI 代码:

package com.ggl.testing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;

public class HomeStylePizza implements Runnable {

private static final Insets bottomInsets = new Insets(10, 10, 10, 10);
private static final Insets normalInsets = new Insets(10, 10, 0, 10);

// GUI components
private JCheckBox cbPepproni, cbSausage, cbMushrooms, cbPineapple, cbOnion,
cbBellPepper;

private JRadioButton rbSmall, rbMedium, rbLarge, rbThinCrust,
rbMediumCrust, rbPan;

private JTextArea textArea;

public static void main(String[] args) {
SwingUtilities.invokeLater(new HomeStylePizza());
} // End of main

@Override
public void run() {
JFrame frame = new JFrame("Pizza Shop");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);

JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());

int gridy = 0;

addComponent(mainPanel, createTitlePanel(), 0, gridy++, 2, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);

addComponent(mainPanel, createToppingPanel(), 0, gridy, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);

JPanel eastPanel = new JPanel();
eastPanel.setLayout(new BorderLayout());
eastPanel.add(createSizePanel(), BorderLayout.WEST);
eastPanel.add(new JLabel(" "), BorderLayout.CENTER);
eastPanel.add(createTypePanel(), BorderLayout.EAST);
eastPanel.add(createButtonPanel(), BorderLayout.SOUTH);

addComponent(mainPanel, eastPanel, 1, gridy++, 1, 1, normalInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

addComponent(mainPanel, createTextAreaPanel(), 0, gridy++, 2, 1,
bottomInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);

frame.add(mainPanel);
frame.pack();
frame.setVisible(true);
}

private JPanel createTitlePanel() {
JPanel panel = new JPanel();

JLabel lblWelcome = new JLabel("Welcome to Home Style Pizza Shop");
Font titleFont = lblWelcome.getFont().deriveFont(20F);
lblWelcome.setFont(titleFont);
lblWelcome.setForeground(Color.RED);

panel.add(lblWelcome);

return panel;
}

private JPanel createToppingPanel() {
Border redBorder = BorderFactory.createLineBorder(Color.RED, 2);
Border emptyBorder = BorderFactory.createEmptyBorder(4, 10, 4, 10);

JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createCompoundBorder(redBorder,
emptyBorder));
panel.setLayout(new GridLayout(0, 1));

JLabel lblEachTopping = new JLabel("Each Topping: $1.50");
lblEachTopping.setForeground(Color.RED);
panel.add(lblEachTopping);

cbPepproni = new JCheckBox("Pepperoni");
cbSausage = new JCheckBox("Sausage");
cbMushrooms = new JCheckBox("Mushrooms");
cbPineapple = new JCheckBox("Pineapple");
cbOnion = new JCheckBox("Onion");
cbBellPepper = new JCheckBox("Bell Pepper");

// add components to optionBox1
panel.add(cbPepproni);
panel.add(cbSausage);
panel.add(cbMushrooms);
panel.add(cbPineapple);
panel.add(cbOnion);
panel.add(cbBellPepper);

return panel;
}

private JPanel createSizePanel() {
Border redBorder = BorderFactory.createLineBorder(Color.RED, 2);
Border emptyBorder = BorderFactory.createEmptyBorder(4, 10, 4, 10);

JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createCompoundBorder(redBorder,
emptyBorder));
panel.setLayout(new GridLayout(0, 1));

JLabel lblPizzaSize = new JLabel("Pizza Size");
lblPizzaSize.setForeground(Color.RED);
panel.add(lblPizzaSize);

rbSmall = new JRadioButton("Small: $6.50");
rbMedium = new JRadioButton("Medium: $8.50");
rbLarge = new JRadioButton("Large: $10.00");

// Create new ButtonGroup
ButtonGroup group = new ButtonGroup();

group.add(rbSmall);
group.add(rbMedium);
group.add(rbLarge);

panel.add(rbSmall);
panel.add(rbMedium);
panel.add(rbLarge);

return panel;
}

private JPanel createTypePanel() {
Border redBorder = BorderFactory.createLineBorder(Color.RED, 2);
Border emptyBorder = BorderFactory.createEmptyBorder(4, 10, 4, 10);

JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createCompoundBorder(redBorder,
emptyBorder));
panel.setLayout(new GridLayout(0, 1));

JLabel lblPizzaType = new JLabel("Pizza Type");
lblPizzaType.setForeground(Color.RED);
panel.add(lblPizzaType);

rbThinCrust = new JRadioButton("Thin Crust");
rbMediumCrust = new JRadioButton("Medium Crust");
rbPan = new JRadioButton("Pan");

// Create new ButtonGroup
ButtonGroup group = new ButtonGroup();

// add components to optionBox2 and grp1
group.add(rbThinCrust);
group.add(rbMediumCrust);
group.add(rbPan);

panel.add(rbThinCrust);
panel.add(rbMediumCrust);
panel.add(rbPan);

return panel;
}

private JPanel createButtonPanel() {
JPanel panel = new JPanel();

JButton btnProcessSelection = new JButton("Process Selection");
btnProcessSelection.addActionListener(new CalculateButtonHandler());
panel.add(btnProcessSelection);

return panel;
}

private JPanel createTextAreaPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());

JLabel lblYourOrder = new JLabel("Your order:");
panel.add(lblYourOrder, BorderLayout.NORTH);

textArea = new JTextArea(6, 12);
JScrollPane scrollPane = new JScrollPane(textArea);
panel.add(scrollPane, BorderLayout.CENTER);

return panel;
}

private void addComponent(Container container, Component component,
int gridx, int gridy, int gridwidth, int gridheight, Insets insets,
int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0);
container.add(component, gbc);
}

private class CalculateButtonHandler implements ActionListener {

public void actionPerformed(ActionEvent event) {

}

}

} // End of HomeStylePizza class

关于java - 我应该使用什么布局来使它适用于 Java?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29975505/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com