gpt4 book ai didi

java - GUI - 将 CardLayout 用于一组类似向导的窗口

转载 作者:行者123 更新时间:2023-11-29 08:09:16 24 4
gpt4 key购买 nike

我有几个窗口,就像向导一样。其中一些使用标准的 BorderLayout,一些使用 TableLayout。如果单击“下一步”按钮,将出现下一个窗口。我读到最好的方法是使用 CardLayout。但我不知道我应该如何将它们“链接”在一起。这是第一个窗口:

public class Identification extends JPanel implements ActionListener {

static String next = "Next";
JButton nextButton;
final static int BIG_BORDER = 75;
final static int SMALL_BORDER = 10;
final static int ELEMENTsLENGHT = 320;
final static int VERTICAL_SPACE = 10;
final static int VERTICAL_SPACE_PLUS = 25;
final static int HORIZONTAL_SPACE = 75;
final static int SPACEforELEMENT_LABEL = 50;
final static int SPACEforELEMENT_TEXT = 40;
final static int H_SPACEforBUTTON = 64;
final static int V_SPACEforBUTTON = 26;
private JTextField nameField = new JTextField();
private JTextField surnameField = new JTextField();


public Identification() {
init();
}

public void init() {

JLabel nameLabel = new JLabel("Please enter your name:");
JLabel surnameLabel = new JLabel("Please enter your surname:");

nameLabel.setForeground(Color.green);
surnameLabel.setForeground(Color.green);
Font labelFont = new Font("SansSerif", Font.PLAIN, 24);
nameLabel.setFont(labelFont);
surnameLabel.setFont(labelFont);

nameField.addActionListener(this);
surnameField.addActionListener(this);

Font TfieldFont = new Font("SansSerif", Font.PLAIN, 16);
nameField.setFont(TfieldFont);
surnameField.setFont(TfieldFont);

nextButton = new JButton("NEXT");
nextButton.setActionCommand(next);
nextButton.addActionListener(this);
nextButton.setToolTipText("Click this button to continue.");

JPanel panelButton = new JPanel();
panelButton.add(nextButton);

double size[][] = {
{ BIG_BORDER, ELEMENTsLENGHT, HORIZONTAL_SPACE,
H_SPACEforBUTTON, SMALL_BORDER }, // Columns
{ BIG_BORDER, SPACEforELEMENT_LABEL, VERTICAL_SPACE,
SPACEforELEMENT_TEXT, VERTICAL_SPACE_PLUS,
SPACEforELEMENT_LABEL, VERTICAL_SPACE,
SPACEforELEMENT_TEXT, VERTICAL_SPACE_PLUS,
SPACEforELEMENT_LABEL, VERTICAL_SPACE,
V_SPACEforBUTTON, SMALL_BORDER } }; // Rows

setLayout(new TableLayout(size));
setBackground(Color.darkGray);

add(nameLabel, "1,1,1,1");
add(nameField, "1,3,1,1");
add(surnameLabel, "1,5,1,1");
add(surnameField, "1,7,1,1");
add(nextButton, "3,11,1,1");

} // end init

public static void createAndShowGUI() {
JFrame frame = new JFrame("Identification");
frame.getContentPane().add(new Identification());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(550, 450);
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {

if (e.getActionCommand().equalsIgnoreCase(next)) {
Identification.showNextWindow();
}
}

public static void showNextWindow() {
// instead of println - show the next window
System.out.println("go to the next window");
}

public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}


} // end class Id

这是第二个:

    public class NewUser extends JPanel implements ActionListener {

static String yes = "<html><font size=\"+1\"><font color=\"#00FF00\">Yes</font></html>";
static String no = "<html><font size=\"+1\"><font color=\"#00FF00\">No</font></html>";
static String next = "Next";
JRadioButton yesButton;
JRadioButton noButton;
JButton nextButton;
boolean choice = false;
boolean isNew;

public NewUser() {
super(new BorderLayout());
init();
}


public void init() {

yesButton = new JRadioButton(yes);
yesButton.setOpaque(false);
yesButton.setActionCommand(yes);
noButton = new JRadioButton(no);
noButton.setOpaque(false);
noButton.setActionCommand(no);
ButtonGroup group = new ButtonGroup();
group.add(yesButton);
group.add(noButton);
nextButton = new JButton("NEXT");
nextButton.setActionCommand(next);
yesButton.addActionListener(this);
noButton.addActionListener(this);
nextButton.addActionListener(this);
nextButton.setToolTipText("Click this button to continue.");

String q = "Hello name surname,<br>Have you used this software tool before?";

JLabel textLabel = new JLabel(
"<html><div style=\"text-align: center;\">" + q + "</html>",
SwingConstants.CENTER);
textLabel.setForeground(Color.green);
Font labelFont = new Font("SansSerif", Font.PLAIN, 30);
textLabel.setFont(labelFont);
textLabel.setBorder(new EmptyBorder(75, 0, 0, 0)); // top, left, //
// bottom, right
setBackground(Color.darkGray);
add(textLabel, BorderLayout.NORTH);

JPanel radioPanel = new JPanel(new GridLayout(2, 1, 0, 0));

radioPanel.setOpaque(false);
radioPanel.add(yesButton);
radioPanel.add(noButton);
radioPanel.setBackground(Color.darkGray);
radioPanel.setBorder(new EmptyBorder(50, 200, 125, 0));

add(radioPanel, BorderLayout.CENTER);

JPanel btnPanel = new JPanel();

btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.LINE_AXIS));
btnPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
btnPanel.add(Box.createHorizontalGlue());

btnPanel.add(nextButton);
btnPanel.setBackground(Color.darkGray);
btnPanel.setAlignmentX(RIGHT_ALIGNMENT);
add(btnPanel, BorderLayout.SOUTH);

} // end init

public static void showNextWindow() {
// instead of println - show the next window
System.out.println("go to the next window");
}


public void actionPerformed(ActionEvent e) {

if (e.getActionCommand().equalsIgnoreCase(yes)) {
System.out.println("You clicked yes");
isNew = false;
yesButton.setEnabled(false);
noButton.setEnabled(true);
choice = true;
} else if (e.getActionCommand().equalsIgnoreCase(no)) {
System.out.println("You clicked no");
isNew = true;
noButton.setEnabled(false);
yesButton.setEnabled(true);
choice = true;
} else if (e.getActionCommand().equalsIgnoreCase(next)) {
// go to the next window
if (choice == true) {
if (isNew == true) {
System.out.println("REGISTERING.");
} else {
System.out.println("LOGGING.");
}
showNextWindow();
}
}
}

public static void createAndShowGUI() {
JFrame frame = new JFrame("New user?");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new NewUser());
frame.setSize(550, 450);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}

}

第三个:

public class Password extends JPanel implements ActionListener {

static JTextArea passwordField;
JLabel infoLabel;
JLabel infoLabel2;
static String next = "Next";
static String copy = "Copy";
JButton nextButton;
JButton copyButton;
final static int BIG_BORDER = 70;
final static int SMALL_BORDER = 10;
final static int ELEMENTsLENGHT = 370;
final static int VERTICAL_SPACE = 15;
final static int VERTICAL_SPACE_PLUS = 25;
final static int HORIZONTAL_SPACE = 30;
final static int SPACEforELEMENT_LABEL = 80;
final static int SPACEforELEMENT_TEXT = 40;
final static int H_SPACEforBUTTON = 64;
final static int V_SPACEforBUTTON = 26;

public Password() {
init();
}

public void init() {

String text = "This is your temporary password generated by the system:";
String text2 = "Remember:<br>it expires in 30 seconds time.";

JLabel infoLabel = new JLabel(
"<html><div style=\"text-align: center;\">" + text + "</html>",
SwingConstants.CENTER);

JLabel infoLabel2 = new JLabel(
"<html><div style=\"text-align: center;\">" + text2 + "</html>",
SwingConstants.CENTER);

infoLabel.setForeground(Color.green);
infoLabel2.setForeground(Color.green);
Font labelFont = new Font("SansSerif", Font.PLAIN, 24);
infoLabel.setFont(labelFont);
infoLabel2.setFont(labelFont);

passwordField = new JTextArea("password1");
passwordField.setEditable(false);

Font TextfieldFont = new Font("SansSerif", Font.PLAIN, 32);
passwordField.setFont(TextfieldFont);

nextButton = new JButton("NEXT");
nextButton.setActionCommand(next);
nextButton.addActionListener(this);
nextButton.setToolTipText("Click this button to continue.");

copyButton = new JButton("Copy");
copyButton.setActionCommand(copy);
copyButton.addActionListener(this);
copyButton.setToolTipText("Click this button to copy the password.");

JPanel panelButton = new JPanel();
panelButton.add(nextButton);

double size[][] = {
{ BIG_BORDER, ELEMENTsLENGHT, HORIZONTAL_SPACE,
H_SPACEforBUTTON, SMALL_BORDER }, // Columns

{ BIG_BORDER, SPACEforELEMENT_LABEL, VERTICAL_SPACE_PLUS,
SPACEforELEMENT_TEXT, VERTICAL_SPACE_PLUS,
SPACEforELEMENT_LABEL, VERTICAL_SPACE_PLUS,
VERTICAL_SPACE, VERTICAL_SPACE_PLUS,
V_SPACEforBUTTON, SMALL_BORDER } }; // Rows

setLayout(new TableLayout(size));
setBackground(Color.darkGray);

add(infoLabel, "1,1,1,1");
add(passwordField, "1,3,1,1");
add(infoLabel2, "1,5,1,1");
add(copyButton, "3,3,1,1");
add(nextButton, "3,9,1,1");

} // end init()

public static void showNextWindow() {
// instead of println - show the next window
System.out.println("go to the next window");
}


@Override
public void actionPerformed(ActionEvent e) {

if (e.getActionCommand().equalsIgnoreCase(next)) {
showNextWindow();
}
if (e.getActionCommand().equalsIgnoreCase(copy)) {
passwordField.copy();
System.out.println("copied");
}
}

public static void createAndShowGUI() {
JFrame frame = new JFrame("RandomSystemPassword");
frame.getContentPane().add(new Password());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(550, 450);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}


public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

抱歉,对于很多代码,我想您可以用更好的方式编写它,但我只是 Java 的新手...任何帮助将不胜感激。

最佳答案

But I don't know how should I "link" them together.

您需要一个框架包含 3 个面板,而不是 3 个框架每个包含一个面板。框架的内容 Pane 将使用 CardLayout。每个单独的面板都可以使用他们想要的任何布局。

首先阅读 How to Use Card Layout 上的 Swing 教程一个工作的例子。在每个面板上,您的下一个/上一个按钮需要显示应该显示的下一个/上一个面板。

一种方法是在每个按钮的 ActionListener 中对面板名称进行硬编码。

另一种方法是使用 CardLayout 类的 next()/previous() 方法。

编辑:

例如,要使 card1 成为一个单独的源文件,您可以这样做:

// JPanel card1 = new JPanel();
// card1.setName("buttons");
// card1.add(new JButton("Button 1"));
// card1.add(new JButton("Button 2"));
// card1.add(new JButton("Button 3"));
JPanel card1 = new ButtonPanel();

ButtonPane 的源代码类似于:

public class ButtonPanel extends JPanel
{
public ButtonPanel()
{
setName("buttons");
add(new JButton("Button 1"));
add(new JButton("Button 2"));
add(new JButton("Button 3"));
}
}

关于java - GUI - 将 CardLayout 用于一组类似向导的窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9305566/

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