마법사와 같은 방식으로 몇 가지 창을 사용할 수 있습니다. 그들 중 일부는 표준 BorderLayout, 일부 TableLayout을 사용하고 있습니다. 다음 버튼을 클릭하면 다음 창이 나타납니다. 나는 CardLayout을 사용하는 최선의 방법을 읽었다. 그러나 나는 어떻게 그들을 "연결"해야하는지 모른다. , 당신이 쓸 수있는 것 같아요GUI - 마법사와 같은 창 세트에 CardLayout 사용
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();
}
});
}
}
죄송합니다, 코드에 대한 많은 :
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();
}
});
}
}
그리고 세 번째이 첫 번째 창입니다 더 나은 방법으로,하지만 난 그냥 Java에 새로운 ... 어떤 도움을 주시면 감사하겠습니다.
1) 'TableLayout'이란 무엇입니까? 2) 더 빨리 도움을 받으려면 [SSCCE] (http://sscce.org/)를 게시하십시오. 3) 질문이 있으십니까? –
TableLayout은 Excel과 같은 레이아웃을 만들 수있는 라이브러리입니다. 행과 열의 크기와 수를 지정합니다. 윈도우가 다른 레이아웃을 사용하고 별도의 클래스에있는 경우 CardLayout을 사용하여 윈도우를 연결하는 방법을 모르겠습니다. (아마도 더 좋은 방법으로 프로그램을 구성 할 수 있었을 것입니다.하지만 다른 것입니다.) – Hurdler