从另一个java类而不是同一个java类获取字符串信息

时间:2015-01-16 17:55:56

标签: java string

所以我有2个类,1个用于非常小的接口,1个用于某些计算。基本上我想在一个盒子里显示一个字符串。已经完成但现在我需要从我的其他课程中获取字符串。这就是我的问题所在,如果我的代码如下所示,我该怎么做:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities; 
import javax.swing.UIManager;


public class SwingJPanelDemo extends JFrame { //where I want the string to be send to from the other class
    String letter = "apple"; //teststring
    private JLabel LetterTest = new JLabel(letter); //where I use the string
    private JButton NextButton = new JButton("Next");
    private JButton NoButton = new JButton("No");

    public SwingJPanelDemo() {
        super("Is This Your Letter?");

        JPanel newPanel = new JPanel(new GridBagLayout());

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.anchor = GridBagConstraints.WEST;
        constraints.insets = new Insets(10, 10, 10, 10);

        constraints.gridx = 0;
        constraints.gridy = 0;     
        newPanel.add(LetterTest, constraints);

        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.CENTER;
        newPanel.add(NextButton, constraints);

        constraints.gridx = 0;
        constraints.gridy = 3;
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.CENTER;
        newPanel.add(NoButton, constraints);

        newPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Is This Your Letter?"));

        add(newPanel);

        pack();
        setLocationRelativeTo(null);
    }

public static void main(String[] args) {
    // set look and feel to the system look and feel
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new SwingJPanelDemo().setVisible(true);
        }
    });
}
}

我将如何获取此字符串?

1 个答案:

答案 0 :(得分:0)

如果您的其他课程如下:

public class Other {

    public static String text = "some text";

}

您可以使用Other.text来获取字符串。