1

さて、さまざまな方法を試しましたが、ボタンを画像に変更できません。ファイルソースが正しいことはわかっています。なぜなら、それを単なる画像としてアップロードすると正常に機能するからです。これが私のコードです:

ImageIcon start = new ImageIcon("start.png");
    button = new JButton (start);

    add(button);

私のコード全体

import java.awt.AWTException;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferInt;
import java.awt.image.WritableRaster;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class UnfollowGUI extends JFrame{

    private JLabel label;
    private JButton button;

    private ImageIcon bgi;
    private JLabel bgl;

    public static Rectangle gameSquare;

    public static boolean rock = true;
    public static boolean runningMine = true;
    public static int stray = 0;
    public static int strayCount = 0;


    public static void main(String[] args) {
        UnfollowGUI gui = new UnfollowGUI ();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when click x close program
        //gui.setSize(886, 266);
        gui.pack();
        gui.setVisible(true);
        gui.setTitle("Solid Cloud Inc - Twitter Unfolower");
    }

    public UnfollowGUI(){

        setLayout(new FlowLayout());

        bgi = new ImageIcon(getClass().getResource("tu.png"));
        bgl = new JLabel (bgi);
        add(bgl);

        ImageIcon start = new ImageIcon("start.png");
        button = new JButton (start);

        add(button);

        label = new JLabel ("");
        add(label);

        Events e = new Events();
        button.addActionListener(e);
    }

    public class Events implements ActionListener {


        public void actionPerformed(ActionEvent e) {

            if (e.getSource() == button) {
               label.setText("Searching");
               try {
                Unfollow();
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            }
    }

    }
public static void Unfollow() throws InterruptedException{

        if (rock==true){
            runningMine = true;

        }
        if (strayCount >=2){
            runningMine = false;
            stray = 90000000;
            JOptionPane.showMessageDialog(null, "Program Ended");
            System.out.println("Program ended");
        }


        //System.out.println("look iron start");

        try {
            Robot robot = new Robot();

            while(runningMine == true){
                //while (rock == true)
                //System.out.println("searching");

                gameSquare = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); //== game square
                BufferedImage img = robot.createScreenCapture(gameSquare);
                WritableRaster r = img.getRaster();
                DataBuffer db = r.getDataBuffer();
                DataBufferInt dbi = (DataBufferInt)db;
                int[] data = dbi.getData();                 

                for (int x_scale = 0; x_scale < gameSquare.width; x_scale += 1) {
                    for(int y_scale = 0; y_scale < gameSquare.height; y_scale += 1) {
                        int rgb = data[x_scale + gameSquare.width * y_scale];
                        if (stray < 10000000){           // 2 sec                                                        

                            if ( rgb == -15817767){ //finds unfollow

                            if (runningMine == true){

                                runningMine = false;
                                stray = 0;
                                int xRock = gameSquare.x + x_scale; // sets the x and y of the point
                                int yRock = gameSquare.y + y_scale;
                                robot.mouseMove(xRock, yRock);

                                robot.mousePress(InputEvent.BUTTON1_MASK);
                                robot.mouseRelease(InputEvent.BUTTON1_MASK);
                                Thread.sleep(800);
                                strayCount = 0;
                                Unfollow();
                                //System.out.println("clicked on Rock");


                            }

                        }
                        else{
                            stray ++;
                            //lookIron();
                        }
                    }
                        else if ((stray>=10000000)&&(stray <20000000)){
                            //skillScreen();
                            //System.out.println("no unfollow buttons");
                            robot.mouseMove(1359, 704);
                            Thread.sleep(500);
                            robot.mousePress(InputEvent.BUTTON1_MASK);
                            robot.mouseRelease(InputEvent.BUTTON1_MASK);
                            Thread.sleep(1000);
                            stray = 0;
                            strayCount ++;
                            Unfollow();


                        }

                    }
                }
            }
        }
        catch(AWTException e) {
            e.printStackTrace();
        }

    }
}
4

2 に答える 2

2

JLabel のアイコンを取得するのと同じ方法で、ボタンの ImageIcon を取得します。だからというより

ImageIcon start = new ImageIcon("start.png");

あなたがするだろう:

ImageIcon start = new ImageIcon(getClass().getResource("start.png"));
button = new JButton (start);

tu.png 画像と同じように、start.png 画像がクラス ファイルと共に配置されていると仮定します。

このコードのビットは私を心配しています:

   try {
        Robot robot = new Robot();

        while(runningMine == true){

           //....

        }

Swing Event Dispatch Thread (または EDT) を結びつけ、アプリケーションをフリーズさせるようです。そのコードは、SwingWorker などのバックグラウンド スレッドで実行する必要がありますか?

于 2013-06-09T14:46:54.437 に答える
-2

画像のサイズを小さくします。つまり、ボタンのサイズと同じにすると、ボタンに画像が表示されます。

それが役立つことを願っています。

于 2014-04-12T12:16:47.460 に答える