我不确定如何标题这个问题,所以如果你有更好的问题,请告诉我。
我的问题是关于JFrames上的对象。我给了一个名为ThreeButtonFrame
的类,看起来像这样:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/** ThreeButtons and ThreeButtonFrame supplier Classes
* Author: David D. Riley
* Date: April, 2004
*
* ThreeButtons supports button events for the ThreeButtonFrame class.
* This class is designed to be inherited and its methods overridden.
*/
public abstract class ThreeButtons {
/** The method below is an event handler for button clicks on
* the LEFT button of an object of type A3ButtonWindow
*/
public abstract void leftAction();
/** The method below is an event handler for button clicks on
* the MID button of an object of type A3ButtonWindow
*/
public abstract void midAction();
/** The method below is an event handler for button clicks on
* the RIGHT button of an object of type A3ButtonWindow
*/
public abstract void rightAction();
/** The class below provides a JFrame that includes three JButtons (left, mid and right).
* The event handling of these three buttons will be performed by the leftAction
* midAction and rightAction methods of the subclass of ThreeButtons.
*/
protected class ThreeButtonFrame extends JFrame implements ActionListener{
private JButton leftButton, midButton, rightButton;
public ThreeButtonFrame(String s) {
super(s);
setBounds(20, 20, 600, 500);
setVisible(true);
Container pane = getContentPane();
pane.setLayout(null);
leftButton = new JButton("LEFT");
leftButton.setBounds(100, 430, 100, 30);
leftButton.addActionListener(this);
pane.add(leftButton, 0);
midButton = new JButton("MID");
midButton.setBounds(250, 430, 100, 30);
midButton.setText("MID");
midButton.addActionListener(this);
pane.add(midButton, 0);
rightButton = new JButton("RIGHT");
rightButton.setBounds(400, 430, 100, 30);
rightButton.addActionListener(this);
pane.add(rightButton, 0);
pane.repaint();
}
public void setBackground(Color c) {
super.getContentPane().setBackground(c);
}
/** Event Handler
* This method is called whenever any of the three
* buttons is clicked
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource() == leftButton)
leftAction();
else if (e.getSource() == midButton)
midAction();
else if (e.getSource() == rightButton)
rightAction();
}
}
}
我接受了Rectangle
课程:
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
/** Rectangle Supplier Class
* Author: David D. Riley
* Date: April, 2004
*/
public class Rectangle extends JComponent {
/** post: getX() == x and getY() == y
* and getWidth() == w and getHeight() == h
* and getBackground() == Color.black
*/
public Rectangle(int x, int y, int w, int h) {
super();
setBounds(x, y, w, h);
setBackground(Color.black);
}
/** post: this method draws a filled Rectangle
* and the upper left corner is (getX(), getY())
* and the rectangle's dimensions are getWidth() and getHeight()
* and the rectangle's color is getBackground()
*/
public void paint(Graphics g) {
// ((Graphics2D)g).setClip( new Ellipse2D.Double(getX(), getY(), getWidth(), getHeight()) );
g.setColor( getBackground() );
g.fillRect(0, 0, getWidth()-1, getHeight()-1);
paintChildren(g);
}
}
然后我创建了一个名为Analyzer
的类,它扩展了Rectangle
类,在这个类中我初始化了2 JLabel
,JTextField
。
Analyzer
:
import javax.swing.*;
import java.awt.*;
/**
* Created by Sully on 4/5/16.
*/
public class Analyzer extends Rectangle {
private JLabel topLabel;
private JLabel resultLabel;
private JTextField text;
public Analyzer(int x, int y, int w, int h) {
super(x, y, w, h);
topLabel = new JLabel("Enter your sentence below: ");
topLabel.setBounds(getX(), getY() - 25, 225, 30);
topLabel.setBackground(Color.blue);
text = new JTextField("BLAHBLAH");
text.setBounds(topLabel.getX() + 2, topLabel.getY() + 30, 350, 20);
text.setBackground(Color.CYAN);
resultLabel = new JLabel("Results: ");
resultLabel.setBounds(text.getX() - 5, text.getY() + 20, 150, 30);
resultLabel.setBackground(Color.MAGENTA);
setBackground(Color.white);
add(topLabel);
add(text);
add(resultLabel);
repaint();
}
public void paint(Graphics g) {
// ((Graphics2D)g).setClip( new Ellipse2D.Double(getX(), getY(), getWidth(), getHeight()) );
g.setColor(getBackground());
g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
paintChildren(g);
}
}
在我初始化ThreeButtonFrame
对象并将Analyzer对象添加到框架的驱动程序类中,尽管我在驱动程序类中创建了3个单独的对象,如下所示:
Analyzer a1 = new Analyzer(win.getX() + 1, win.getY() + 10, 450, 100);
Analyzer a2 = new Analyzer(a1.getX() + 25, a1.getY() + (30 + a1.getHeight()), 450, 100);
Analyzer a3 = new Analyzer(a2.getX() + 25, a2.getY() + (30 + a2.getHeight()), 450, 100);
win.add(a1, 0);
win.add(a2, 0);
win.add(a3, 0);
win.repaint();
只有第一个电话(a1)可见。剩下的2个白色框被添加但没有元素。这是为什么?
我尝试将Analyzer
类中的变量更改为构造函数方法中的实例变量。