【发布时间】:2015-12-04 04:31:17
【问题描述】:
我正在创建一个 Swing 应用程序。我创建了一个名为Drone 的类,并在另一个名为UserAccount 的类中创建了它的对象。在JButton,我正在使用userAccount 调用Drone 类的getter,但没有执行任何操作。为什么会这样?
这是Drone类的代码
private int x;
private int y;
private String text;
private int a;
private int b;
private String text1;
private int c;
private int d;
private String text2;
public Drone() {
x= 10;
y=150;
a=10;
b=150;
c=1000;
d=150;
text="Drone";
text1="Factory";
text2="Hospital";
setSize(1070,300);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRect(0, 0, 1070, 300);
g.setColor(Color.BLACK);
g.drawString(text, x, y);
System.out.println(x + "" + y);
g.drawString(text1, a, b);
g.drawString(text2, c, d);
}
public void factory() throws InterruptedException{
while(true){
while(a<=getWidth()){
b=getHeight()/2;
repaint();
Thread.sleep(1000000);
}
}
}
public void Hospital() throws InterruptedException{
while(true){
while(c<=getWidth()){
d=getHeight()/2;
repaint();
Thread.sleep(1000000);
}
}
}
public void start() throws InterruptedException{
while(true){
while(x <= getWidth()-50){
x++;
y = getHeight()/2;
repaint();
Thread.sleep(150);
}
while(x >= 0){
x--;
y = getHeight()/2;
repaint();
Thread.sleep(150);
}
break;
}
}
public static void main(String[] args) throws InterruptedException{
JFrame frame = new JFrame("Drone Movement");
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
Drone drone = new Drone();
frame.getContentPane().add(drone);
frame.setSize(1070,300);
frame.setLocation(180,200);
frame.setVisible(true);
drone.start();
drone.factory();
drone.Hospital();
}
我在JButton 中的actionPerformed 呼叫这个课程,在JPanel 中出现
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
userAccount.getDrone();
}
为什么这里没有执行任何操作?
【问题讨论】:
-
什么动作?什么按钮?没有任何代码为您的问题提供任何上下文。考虑提供一个runnable example 来证明您的问题。这不是代码转储,而是您正在做的事情的一个示例,它突出了您遇到的问题。这将导致更少的混乱和更好的响应
-
假设
Drone在基于 Swing 的组件中,那么您真的不应该覆盖paint,而是更喜欢paintComponent。您还应该调用super绘制方法以维护现有的绘制链合约 -
我们可能永远不会知道...
标签: java swing class object jbutton