按钮和绘画方法

时间:2015-06-15 03:13:29

标签: java swing graphics jbutton actionlistener

我的按钮出现问题。我知道他们正在工作,因为我已经通过System.exit退出问题来测试它们。这是我的输出:

http://imgur.com/Ks7mIFa

单击关闭按钮时,开关上的手柄应重绘到另一侧,关闭按钮应更改为打开。当我点击打开按钮时,它应该相反。但是,按钮不做任何事情。我做错了什么?

public class ProgrammingAssignment2 {

public static void main(String[] args) {

    boolean ison = false;

    // Objects
    Circuit circuitObject = new Circuit();
    Controller controllerObject = new Controller();
    Draw drawObject = new Draw();
    AllListeners listenerObject = new AllListeners();
    drawObject.window();

    circuitObject.buttons(drawObject, ison);
    controllerObject.openFile("Programming Assignment 2 Data.txt", drawObject);
}
}

类电路只创建按钮

import javax.swing.JButton;

public class Circuit {

public void buttons(Draw drawObject, boolean ison) {
    AllListeners listenerObject = new AllListeners();

    if (ison == true) {

        JButton openButton = new JButton("Close");
        openButton.addActionListener(listenerObject);
        openButton.setBounds(200, 100, 50, 20);
        drawObject.add(openButton);
    } else if (ison == false) {

        JButton closeButton = new JButton("Open");
        closeButton.addActionListener(listenerObject);
        closeButton.setBounds(50, 100, 50, 20);
        drawObject.add(closeButton);
    }
}
}

Draw类完成大部分工作。它会创建所有图形并在具有每个对象标题的文本文件中读取(如开关和灯泡)。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JFrame;
import java.awt.Graphics;

public class Draw extends JFrame {

private String[] line = new String[5];
private int counter = 0;
private boolean ison;

public void window() {

    setSize(500, 500);
    setTitle("Programming Assignment 2");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}

public void readFile(String filename) {

    counter = 1;
    BufferedReader br = null;

    try {
        br = new BufferedReader(new FileReader(filename));


        for (int i = 0; i < 4; i++) {
            line[i] = br.readLine();
        }

    } catch (FileNotFoundException e) {
        String error = "File was not found";
        System.out.println(error.toString());
        System.out.println("or could not be opened.");
    } catch (IOException e) {
        System.out.println("Error reading from file.");
    } finally {
        try {
            br.close();
        } catch (IOException e) {
            System.out.println("Error closing file.");
        }
    }
}

public void paint(Graphics g) {
    Circuit circuitObject = new Circuit();
    super.paint(g);

    if (ison == true) {
        turnon(g);
        circuitObject.buttons(this, ison);
    } else {
        turnoff(g);
    }
}

public void setisOn() {
    ison = true;
}

public void setisOff() {
    ison = false;
}

public void turnoff(Graphics g) {
    // Title
    g.drawString(line[0], 150, 40);

    //Switch
    g.drawString(line[2], 130, 190);
    g.drawRect(100, 150, 100, 20);
    g.drawOval(115, 155, 10, 10);
    g.drawOval(175, 155, 10, 10);
    g.drawArc(140, 140, 20, 20, 180, -180);


    //off switch
    g.drawLine(160, 150, 182, 133);
    g.drawLine(157, 142, 173, 128);
    g.drawLine(173, 128, 182, 133);


    //Power Supply
    g.drawString(line[1], 50, 420);
    g.drawRect(50, 320, 50, 80);
    g.drawLine(50, 320, 70, 290);
    g.drawLine(100, 320, 120, 290);
    g.drawLine(70, 290, 120, 290);
    g.drawLine(120, 370, 120, 290);
    g.drawLine(120, 370, 100, 400);
    //plus
    g.drawLine(94, 310, 100, 310);
    g.drawLine(97, 307, 97, 313);
    // minus
    g.drawLine(100, 300, 107, 300);
    // pliers
    g.drawRect(70, 305, 5, 10);
    g.drawRect(90, 288, 5, 10);

    //lightbulb
    g.drawString(line[3], 400, 250);
    g.drawRect(400, 200, 20, 20);
    g.drawOval(395, 170, 30, 30);
    // pliers
    g.drawRect(400, 220, 5, 10);
    g.drawRect(415, 220, 5, 10);

    // plus wire to switch
    g.drawLine(72, 305, 120, 160);
    //bulb to switch
    g.drawLine(180, 160, 400, 230);
    //bulb to minus
    g.drawLine(90, 290, 420, 230);
}

public void turnon(Graphics g) {
    // Title
    g.drawString(line[0], 150, 40);

    //Switch
    g.drawString(line[2], 130, 190);
    g.drawRect(100, 150, 100, 20);
    g.drawOval(115, 155, 10, 10);
    g.drawOval(175, 155, 10, 10);
    g.drawArc(140, 140, 20, 20, 180, -180);

    //on switch
    g.drawLine(140, 150, 122, 133);
    g.drawLine(143, 142, 129, 128);
    g.drawLine(122, 133, 129, 128);

    //Power Supply
    g.drawString(line[1], 50, 420);
    g.drawRect(50, 320, 50, 80);
    g.drawLine(50, 320, 70, 290);
    g.drawLine(100, 320, 120, 290);
    g.drawLine(70, 290, 120, 290);
    g.drawLine(120, 370, 120, 290);
    g.drawLine(120, 370, 100, 400);
    //plus
    g.drawLine(94, 310, 100, 310);
    g.drawLine(97, 307, 97, 313);
    // minus
    g.drawLine(100, 300, 107, 300);
    // pliers
    g.drawRect(70, 305, 5, 10);
    g.drawRect(90, 288, 5, 10);

    //lightbulb
    g.drawString(line[3], 400, 250);
    g.drawRect(400, 200, 20, 20);
    g.drawOval(395, 170, 30, 30);
    // pliers
    g.drawRect(400, 220, 5, 10);
    g.drawRect(415, 220, 5, 10);

    // plus wire to switch
    g.drawLine(72, 305, 120, 160);
    //bulb to switch
    g.drawLine(180, 160, 400, 230);
    //bulb to minus
    g.drawLine(90, 290, 420, 230);
}
}

控制器没有那么正确。

public class Controller {

public void openFile(String filename, Draw drawObject) {
    drawObject.readFile(filename);
}
}

这是actionlisterner类

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AllListeners implements ActionListener {

public void actionPerformed(ActionEvent e) {
    Circuit circuitObject = new Circuit();
    Draw drawObject = new Draw();
    String buttonString = e.getActionCommand();

    if (buttonString.equals("Close")) {
        drawObject.setisOn();
        drawObject.repaint();

    } else if (buttonString.equals("Open")) {
        drawObject.setisOff();
        drawObject.repaint();

    } else {
        System.out.println("Unexpected error.");
    }
}
}

1 个答案:

答案 0 :(得分:3)

您在此代码中遇到了很多重大问题,包括:

  • 在JFrame的绘画方法中直接绘制 ,这会带来一些问题,因为你可能会弄乱JFrame自己复杂的绘画。
  • 将程序逻辑放在绘画方法中,一种你无法完全控制何时或是否会触发的方法,以及一种你不应该放慢速度的方法
  • 在绘画方法中放置组件创建代码。
  • 尝试添加多个JButtons,而不是改变现有组件的状态。
  • 创建多个Circuit对象。
  • 尝试通过setBounds(...)使用绝对定位将组件放置在使用BorderLayout的容器中。

我建议你

  • 重新开始并废弃此代码。
  • 只在JPanel的paintComponent方法中绘制,就像教程告诉你的那样。
  • 只创建一次按钮,然后将其添加到GUI中。
  • 从绘画(此处为paintComponent)方法中获取所有程序逻辑,并且所有对象状态在该方法之外更改,因为它们仅用于绘画和绘画。
  • 相反,逻辑应该属于控制器,应该通知按钮按下。
  • 因此,请考虑按下按钮通知控件推送的内容,
  • 控件更改程序的状态(更改变量)
  • 然后调用repaint,以便paintComponent方法可以使用这些变量来更改其绘图。
  • 另外,尽可能避免使用setBounds和null布局,而是使用布局管理器/边框/嵌套JPanel来帮助您放置组件。
相关问题