我是初学java程序员。我在package1中有两个类。我正在尝试使用Main中另一个类的JLabel,这里有两段代码:
package package1;
import package1.*;
import javax.swing.*;
import java.awt.event.*;
public class Main extends JPanel implements ActionListener {
//Event Text
protected static JLabel eventText;
public void event1() {
eventText = event1.event1EventText;
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Stack Overflow");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Main newContentPane = new Main();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.setSize(1300, 700);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
我的第二个文件只是制作一个JLabel。
package package1;
import javax.swing.*;
public class event1 extends JPanel {
protected static JLabel event1EventText;
public static void main(String[] args) {
event1EventText = new JLabel();
event1EventText.setText("Some text");
}
}
我需要在Main中使用event1EventText。我运行代码时没有出现任何错误,但eventText从不显示。我只是得到一个空窗口。
答案 0 :(得分:0)
哦,我认为问题在于你没有把类Main中的eventText放到createAndShowGUI方法的框架中。
答案 1 :(得分:0)
Try this solution :-
package package1;
import javax.swing.*;
import java.awt.event.*;
public class Main extends JPanel implements ActionListener {
//Event Text
protected static JLabel eventText;
public static void event1() {
eventText = event1.createLabel();
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Stack Overflow");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
event1();
Main newContentPane = new Main();
newContentPane.add(eventText);
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.setSize(1300, 700);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
package package1;
import javax.swing.*;
public class event1 extends JPanel {
protected static JLabel event1EventText;
protected static JLabel createLabel(){
event1EventText = new JLabel();
event1EventText.setText("Some text");
return event1EventText;
}
}
答案 2 :(得分:0)
您的问题有点模糊,解决方案可能因更多上下文而有所不同,但是,通常情况下,如果您在其他组件中有组件,则不希望允许直接访问它们,因为对于其他组件来说太容易了你的程序向他们做了他们真正不应该做的事情。
相反,一种方法是提供setter和getters间接改变标签的状态,例如......
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test100 {
public static void main(String[] args) {
new Test100();
}
public Test100() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
TestPane pane = new TestPane();
pane.setLabelText("Look, I'm a label on another panel");
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(pane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel label;
public TestPane() {
setLayout(new GridBagLayout());
label = new JLabel();
add(label);
}
public void setLabelText(String text) {
label.setText(text);
}
}
}