2016-12-12 38 views
0

我一直有這個困難。基本上我想讓一個用戶在一個Java類中輸入一個名稱,並將這些數據顯示在另一個Java類中。我嘗試使用get,但我一直得到'null'。另外,如果我想從我的班級獲得一個int(例如)以顯示在我的第二堂課,你將如何做到這一點。它基本上是一樣的嗎?當在java中使用JPanels時,如何在一個類中顯示用戶輸入數據到另一個類

下面就來說明我的意思

一等

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);   
    }   
} 
+0

是的。我只是添加了第二堂課來展示我的意思。我想要輸入任何值來轉到下一個JPanel。例如,如果某人在第一個類中寫下'Fred',我希望該值能夠顯示在第二個類上 – Winderd

+0

[在JFrame之間傳遞值的可能的重複](http://stackoverflow.com/questions/7017063/通過值之間的jframes) – Frakcool

+0

謝謝,這是非常有用的,它簡化了一些東西,但我想知道如何將值移動到另一個類(如int)。我打算做一個項目,在其中添加輸入名稱和一個隨機int到一個文件中,以便我可以存儲它。或者將該代碼添加到同一個類中會更有意義? – Winderd

回答

2

我對你的建議:

  1. 不要延長JFrame,而不是延長JPanel。請參閱:Extends Frame vs creating it inside the programWhy shouldn't you extend JFrame and other components
  2. 請勿使用多個JFrames。見The use of multiple JFrames, good/bad practice?BAD),而不是你可能想使用Card LayoutJDialog小號

你的意思是通過擴展來試試呢?你的意思是「公共類Hello擴展了Exampl」嗎?我是新來的java,所以我不太瞭解。

  • 從另一個回答這一評論,你可能希望再進一個GUI應用程序,它增加了更多的複雜性,你的程序,因此你的學習首先學習的基礎知識在控制檯應用程序。
  • 我打算做一個項目,我添加輸入名稱和隨機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 
    
  • +0

    非常感謝。你一直很有幫助。 – Winderd

    0

    請在第一類中的吸氣劑的例子,擴展第二個類,然後使用super.getter ();

    +0

    延期是什麼意思?你的意思是「公共類Hello擴展了Exampl」嗎?我是新來的java,所以我不太瞭解。 – Winderd

    相關問題