有两名玩家将在JTextFields中输入他们的名字。我想要做的是,我从Enter.java中的Welcome框架输入的数据将被传输到ActualGame.java中的JLabel。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
public class Enter extends JFrame implements ActionListener {
private String one = "";
private String two = "";
private JTextField txtOne = new JTextField();
private JTextField txtTwo = new JTextField();
public Enter() {
this.setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Welcome");
setSize(200, 130);
setVisible(true);
setResizable(false);
setLocationRelativeTo(null);
add(txtOne);
add(txtTwo);
enter.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
Main main = new Main();
this.setVisible(false);
one = txtOne.getText();
two = txtTwo.getText();
}
}
Main是保存ActualGame()的JFrame的主类,也是Enter()的主类。
import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
public class Main extends JFrame {
public Main() {
add(new ActualGame());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Main");
setSize(400, 557);
setVisible(true);
setResizable(false);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
Enter enter=new Enter();
}
}
ActualGame:
import java.awt.*;
import javax.swing.*;
public class ActualGame extends JPanel{
private JLabel lblOne = new JLabel(one);//how i wish it would be that easy
private JLabel lblTwo = new JLabel(two);
public ActualGame() {
setLayout(new FlowLayout());
add(lblOne);
add(lblTwo);
}
}
我应该怎么做才能使用从Enter.java到ActualGame.java的String变量一和二? 我是编程特别是java swing的新手和菜鸟。接受批评和建议。谢谢。
答案 0 :(得分:3)
建议:
具体来说,给你的Enter类一个getText方法,允许其他对象查询它的JTextField的状态:
public String getTxtOneText() {
return txtOne.getText();
}
此外,更改您的ActualGame类,以便它可以在需要时接受字符串信息:
class ActualGame extends JPanel {
private JLabel lblOne = new JLabel();
public ActualGame(String text) {
lblOne.setText(text);
setLayout(new FlowLayout());
add(lblOne);
}
public void setLblOneText(String text) {
lblOne.setText(text);
}
}
如,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Foo {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ActualGame actualGame = new ActualGame("");
Main main = new Main(actualGame);
main.pack();
Enter enter = new Enter(main);
enter.setVisible(true);
actualGame.setLblOneText(enter.getTxtOneText());
main.pack();
main.setLocationRelativeTo(null);
main.setVisible(true);
}
});
}
}
class Enter extends JDialog implements ActionListener {
private String one = "";
private JTextField txtOne = new JTextField(10);
private JButton enter = new JButton("Enter");
public Enter(JFrame frame) {
super(frame, "Welcome", true);
this.setLayout(new FlowLayout());
enter.addActionListener(this);
txtOne.addActionListener(this);
add(txtOne);
add(enter);
pack();
setLocationRelativeTo(null);
// this has to be done last
// setVisible(true);
}
public String getTxtOneText() {
return txtOne.getText();
}
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
}
class Main extends JFrame {
ActualGame actualGame;
public Main(ActualGame actualGame) {
super("Main");
this.actualGame = actualGame;
add(actualGame);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class ActualGame extends JPanel {
private JLabel lblOne = new JLabel();
public ActualGame(String text) {
lblOne.setText(text);
setLayout(new FlowLayout());
add(lblOne);
}
public void setLblOneText(String text) {
lblOne.setText(text);
}
}
答案 1 :(得分:0)
尝试将ActualGam作为Enter
的底层import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
public class Enter extends JFrame implements ActionListener {
private String one = "";
private JTextField txtOne = new JTextField();
public Enter() {
this.setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Welcome");
setSize(200, 130);
setVisible(true);
setResizable(false);
setLocationRelativeTo(null);
add(txtOne);
enter.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
Main main = new Main();
this.setVisible(false);
one = txtOne.getText();
}
class ActualGame extends JPanel{
private JLabel lblOne = new JLabel(one);
public ActualGame() {
setLayout(new FlowLayout());
Enter.this.add(lblOne);
}
}
}