我有几个窗户,以类似巫师的方式。其中一些使用标准的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的新手...任何帮助都将不胜感激。
答案 0 :(得分:5)
但我不知道应该如何将它们“链接”在一起。
你需要一个框架有3个面板,而不是3个框架,每个框架有一个面板。框架的内容窗格将使用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"));
}
}