Java Jlabel setText

时间:2014-12-22 14:30:12

标签: jlabel

我是一名java初学者,我使用eclipse制作一个gridbaglayout:

            import java.awt.EventQueue;
            import javax.swing.JFrame;
            import java.awt.GridBagLayout;
            import javax.swing.JLabel;
            import java.awt.GridBagConstraints;

            public class test4 {
                public static JFrame frame;
                /**
                 * Launch the application.
                 */
                public static void main(String[] args) {
                    EventQueue.invokeLater(new Runnable() {
                        public void run() {
                            try {
                                test4 window = new test4();
                                window.frame.setVisible(true);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    }
                /**
                 * Create the application.
                 */
                public test4() {
                    initialize();
                }
                /**
                 * Initialize the contents of the frame.
                 * @return 
                 */
                public static  void initialize() {
                    frame = new JFrame();
                    frame.setBounds(100, 100, 137, 89);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    GridBagLayout gridBagLayout = new GridBagLayout();
                    gridBagLayout.columnWidths = new int[]{0, 0};
                    gridBagLayout.rowHeights = new int[]{0, 0};
                    gridBagLayout.columnWeights = new double[]{0.0, Double.MIN_VALUE};
                    gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE};
                    frame.getContentPane().setLayout(gridBagLayout);

                     JLabel lblNewLabel = new JLabel("Label");
                    GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
                    gbc_lblNewLabel.gridx = 0;
                    gbc_lblNewLabel.gridy = 0;
                    frame.getContentPane().add(lblNewLabel, gbc_lblNewLabel);


                }

            }

我想在{public static void main(String [] args)}或{private static set(){lblNewLabel.setText(&&)中使用{lblNewLabel.setText(" test");} #34; test");}}但它不起作用。我该怎么办?谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

public class MyNiceApplication {

private Label niceLabel;

   void initialize(){...} //Frame init...

   public void setNiceLabelText(String text){
      niceLabel.setText(text);
   }

}


public static void main(String[] args) {
   MyNiceApplication app = new MyNiceApplication();
   app.setNiceLabelText("New LabelText");

}