[英]How can I replace one of two JPanels with another JPanel in Java?
[英]When using JPanels in java how can you display user input data in one class to another
我遇到了一些困難。 基本上,我希望用戶在一個Java類中輸入一個名稱,並在另一個Java類中顯示該數據。 我嘗試使用gets,但我一直保持'null'。 如果我想從我的班級(示例)中獲取一個int以便顯示在我的第二堂課中,那么您將如何做呢? 基本上會一樣嗎?
這是一個例子來說明我的意思
一等艙
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Exampl extends JFrame implements ActionListener {
JLabel intro = new JLabel("Welcome What is your name?");
JTextField answer = new JTextField(10);
JButton button = new JButton("Enter");
final int WIDTH = 330;
final int HEIGHT = 330;
JLabel greeting = new JLabel("");
JButton next =new JButton("Next");
String name = answer.getText();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Exampl() {
super("Welcome");
setSize(WIDTH, HEIGHT);
setLayout(new FlowLayout());
add(intro);
add(answer);
add(button);
add(greeting);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if (source == button) {
String greet = "Hello there" + name;
greeting.setText(greet);
add(next);
next.addActionListener(this);
if(source==next)
dispose();
new Hello();
}
}
public static void main(String[] args) {
Exampl frame= new Exampl();
frame.setVisible(true);
}
}
二等
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Hello extends JFrame implements ActionListener {
String name;
JLabel hi = new JLabel("Nice to see you "+name);
JButton button = new JButton("Enter");
final int WIDTH = 330;
final int HEIGHT = 330;
JLabel greeting = new JLabel("");
JButton next =new JButton("Next");
public Hello() {
super("Welcome");
setSize(WIDTH, HEIGHT);
setLayout(new FlowLayout());
add(hi);
add(button);
add(greeting);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if (source == button) {
String greet = "example " + name;
greeting.setText(greet);
add(next);
}
}
public static void main(String[] args) {
Exampl frame= new Exampl();
frame.setVisible(true);
}
}
我對您的建議:
JFrame
,而是擴展JPanel
。 請參閱: 擴展Frame
與在程序內部創建Frame
,以及為什么不擴展JFrame
和其他組件 JFrames
。 請參閱使用多個JFrames
,好/壞做法? ( BAD )相反,您可能想嘗試使用Card Layout或JDialog
擴展是什么意思? 您是說“公共類Hello擴展Exampl”嗎? 我是Java的新手,所以我並不了解。
我正在計划做一個項目,在其中將輸入的名稱和一個隨機int添加到文件中,以便我可以存儲它。 還是將代碼添加到同一類中更有意義?
從該注釋中,您可以創建一個公共方法,該方法以所需的格式返回JTextField
中的值,然后從另一個類中調用該方法,例如:
public String getUserName() { return userNameField.getText(); } public int getDigit() { try { return Integer.parseInt(digitField.getText()); } catch (NumberFormatException nfe) { nfe.printStackTrace(); } return -1; //In case it's not a number, or return 0, or whatever you need } //------In another class------// int digit = object1.getDigit(); //Where object1 is an instance of the other class where those methods are defined
在第一堂課中做一個吸氣劑,然后在第二堂課中做一個吸氣劑,然后使用super.getter();。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.