我接受了一些建议并重写了一些代码以实现所提出的建议并使其更具可读性。现在它不会编译。编译器抱怨它无法解析 JLabel 上的构造函数。我评论了问题所在。
/**
* Created with IntelliJ IDEA.
* User: goulartj
* Date: 9/4/13
* Time: 10:11 AM
* To change this template use File | Settings | File Templates.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class NewSwing implements ActionListener{
JFrame frame;
JTextField textField;
JTextArea textArea;
JPanel panel;
Image image;
JLabel label;
private final static String newline = "\n";
public static void main(String[] args) {
NewSwing gui = new NewSwing();
gui.go();
}
public void go(){
frame = new JFrame();
frame.getContentPane().setSize(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textField = new JTextField("This is a text field and these are my texticles!");
textField.selectAll();
textArea = new JTextArea();
panel = new JPanel();
image = new ImageIcon("cuteKitten.jpg").getImage();
label = new JLabel(image); //COMPILER COMPLAINS HERE
panel.add(label);
frame.getContentPane().add(BorderLayout.NORTH, textField);
frame.getContentPane().add(BorderLayout.CENTER, textArea);
frame.getContentPane().add(BorderLayout.EAST, panel);
panel.setBackground(Color.CYAN);
textField.addActionListener(this);
}
public void actionPerformed(ActionEvent event){
String text = textField.getText();
textArea.append(text + newline);
textField.selectAll();
}
/* class MyDrawPanel extends JPanel {
public void paintComponent(Graphics g) {
g.drawImage(image, 3, 4, this);
}
} */
}
提前感谢所有帮助!你们总是对我这么好!