无法从静态上下文引用方法

时间:2014-04-10 18:08:18

标签: java bluej

我收到了这个错误:

non-method setBounds(int,int,int,int) cannot be referenced from a static context

我的代码如下:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
 * Write a description of class GUI here.
 * 
 * @author Ibrahim Y. Hmood 
 * @version 04.02.14
 */
public class GUI extends JFrame implements ActionListener {
    /**
     * Identify the components for the window
     */
    JPanel contentPane;
    JLabel lblLastName;
    JLabel lblFirstName;
    JLabel lblStudentID;
    JTextField txtLastName;
    JTextField txtFirstName;
    JTextField txtStudentID;
    JButton    btnSubmit;
    JLabel lblStoreFirstName;
    JLabel lblStoreLastName;
    JLabel lblStoreStudentID;
    JTextField txtStorestudentID;
    JTextArea txaAll;
    JCheckBox chkBox1;
    CheckBoxListener myListener = null;
    /**
     * Constructor for objects of class GUI
     */
public GUI()
{
    /**
     * Create the panel, its components and add them to the panel
     */
    contentPane = new JPanel (null);
    lblLastName = new JLabel ("Enter Last Name");
    lblFirstName = new JLabel ("Enter First Name");
    lblStudentID = new JLabel ("Enter Student ID");
    txtLastName = new JTextField ();
    txtFirstName = new JTextField();
    txtStudentID = new JTextField();
    btnSubmit = new JButton ("Submit Info");
    lblStoreFirstName = new JLabel ();
    lblStoreLastName = new JLabel ();
    lblStoreStudentID = new JLabel ();
    txaAll= new JTextArea ();
    chkBox1 =  new JCheckBox ("Check box one");
    chkBox1.setBounds(200,180,150,20);
    JRadioButton Label  = new JRadioButton ();
    JRadioButton.setBounds (100,200, 150, 20);

    lblLastName.setBounds(25, 25, 590, 20);
    lblFirstName.setBounds (25, 65,590, 20);
    lblStudentID.setBounds (25, 105,590, 20);

    txtLastName.setBounds(140,25,150,20);
    txtFirstName.setBounds(140,65,150,20);
    txtStudentID.setBounds(140,105,150,20);
    txaAll.setBounds(140,345,250,250); 


    btnSubmit.setBounds(100, 130, 250 ,20);

   lblStoreFirstName.setBounds(200,200,150,20);
    lblStoreLastName.setBounds(200, 240, 150, 20);
     lblStoreStudentID.setBounds(200,260, 150, 20);

    contentPane.add(lblLastName);
    contentPane.add(lblFirstName);
    contentPane.add(lblStudentID);

    contentPane.add(txtLastName);
    contentPane.add(txtFirstName);
    contentPane.add(txtStudentID);
    contentPane.add(lblStoreFirstName);
    contentPane.add(lblStoreLastName);
    contentPane.add(lblStoreStudentID);
    contentPane.add(txaAll);

    myListener = new CheckBoxListener();
    chkBox1.addItemListener(new CheckBoxListener());
    contentPane.add(chkBox1);


    chkBox1.setActionCommand("checkbox1");
    chkBox1.addActionListener(this);
     contentPane.add(chkBox1);

    btnSubmit.setActionCommand("Submit");
    btnSubmit.addActionListener(this);
    contentPane.add(btnSubmit);


    setTitle("GUI");
    setContentPane(contentPane);
    setLocation(0,0);
    setSize(600,600);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    private class CheckBoxListener implements ItemListener{
        public void itemStateChanged (ItemEvent e)
        {
            Object source = e.getSource();
            if (source == chkBox1) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    txaAll.setBackground(Color.green);
                }
            }
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if ("Submit".equals(e.getActionCommand())){
        btnSubmit.setBackground(Color.red);
        btnSubmit.setBackground(Color.cyan);
        String lastname = txtLastName.getText();
        String firstname = txtFirstName.getText();
        String studentID = txtStudentID.getText();
        String str = txaAll.getText();
        str+= firstname + "\t" + lastname + "\t" + studentID + "\n";
        lblStoreLastName.setText(lastname);
        lblStoreFirstName.setText (firstname);
        lblStoreStudentID.setText (studentID);
        txaAll.setText(str);      
    }
     else if ("checkBox1".equals(e.getActionCommand()))
        this.setBackground(Color.yellow);
    }
}

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

你把
JRadioButton label = new JRadioButton(); JRadioButton.setBounds(100,200,150,20);
你应该把
label.setBounds(100,200,150,20);

答案 1 :(得分:0)

不能只说JRadioButton.setBounds(100,200,150,20);在JRadioButton上调用setBounds是在静态上下文中调用它。你必须在类的实例上调用它。