我目前正在为一个项目做一个小程序,最终可以在触发某些东西后扫描屏幕。我还差得远呢。
让gui做我想做的事情,让变量进入另一个类,我有点困难。
到目前为止,我有三个班,工人阶级就在那里,这样我就可以知道我要做什么。
基本上,这就是我想做的。我想打开gui,获取这些值,然后根据这些值运行一组特定的命令。我只是无法将这些值传递给其他类和其他一些东西。
下面是我分别在main、myframe和working类中的代码。
我对java有点陌生,我仍在努力学习类的东西,所以我会学习一些基本的东西。希望你们能帮忙。
谢谢!
package rpg;
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.imageio.ImageIO;
public class Main
{
public static void main(String[] args) throws AWTException
{
// JCheckBox = A GUI component that can be selected or deselected
new MyFrame();
//Working working = new Working();
//working.startUp();
}
}
package rpg;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class MyFrame extends JFrame implements ActionListener
{
public int playerArea, playerLevel, hoursRunning;
public String areaOne, levelOne, hoursToRun, workType;
ImageIcon logo;
JButton submit;
JCheckBox checkBox0, checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6, checkBox7, checkBox8;
JTextField area, level, hours;
MyFrame()
{
new JFrame(); //creates a frame
this.setSize(375, 180);
this.setTitle("EPIC RPG HACKS"); //sets title of frame
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //exit from application
this.setResizable(true); //prevent frame from being resized
this.setVisible(true); //make frame visible
this.setLayout(new FlowLayout());
this.setLocationRelativeTo(null);
ImageIcon image = new ImageIcon("rpg.png"); //create an ImageIcon
this.setIconImage(image.getImage()); //change icon of frame
this.getContentPane().setBackground(new Color(0x7C5FF5)); //change color of background
area = new JTextField();
area.setPreferredSize(new Dimension(100,30));
area.setForeground(new Color(0x7C5FF5));
area.setBackground(Color.BLACK);
area.setCaretColor(Color.WHITE);
area.setText("Area");
this.add(area);
level = new JTextField();
level.setPreferredSize(new Dimension(100,30));
level.setForeground(new Color(0x7C5FF5));
level.setBackground(Color.BLACK);
level.setCaretColor(Color.WHITE);
level.setText("Level");
this.add(level);
hours = new JTextField();
hours.setPreferredSize(new Dimension(100,30));
hours.setForeground(new Color(0x7C5FF5));
hours.setBackground(Color.BLACK);
hours.setCaretColor(Color.WHITE);
hours.setText("Hours Running");
this.add(hours);
ButtonGroup bg = new ButtonGroup();
checkBox0 = new JCheckBox(); //rpg hunt
checkBox1 = new JCheckBox(); //rpg adventure
checkBox2 = new JCheckBox(); //rpg training
checkBox3 = new JCheckBox(); //rpg lootbox
checkBox4 = new JCheckBox(); //rpg chop
checkBox5 = new JCheckBox(); //rpg fish
checkBox6 = new JCheckBox(); //rpg pickup
checkBox7 = new JCheckBox(); //rpg mine
checkBox8 = new JCheckBox(); //rpg heal
bg.add(checkBox4);
bg.add(checkBox5);
bg.add(checkBox6);
bg.add(checkBox7);
this.add(checkBox0);
checkBox0.setText("rpg hunt");
this.add(checkBox1);
checkBox1.setText("rpg adventure");
this.add(checkBox2);
checkBox2.setText("rpg training");
this.add(checkBox3);
checkBox3.setText("rpg lootbox");
this.add(checkBox4);
checkBox4.setText("rpg chop");
this.add(checkBox5);
checkBox5.setText("rpg fish");
this.add(checkBox6);
checkBox6.setText("rpg pickup");
this.add(checkBox7);
checkBox7.setText("rpg mine");
this.add(checkBox8);
checkBox8.setText("rpg heal");
checkBox0.setFocusable(false);
checkBox1.setFocusable(false);
checkBox2.setFocusable(false);
checkBox3.setFocusable(false);
checkBox4.setFocusable(false);
checkBox5.setFocusable(false);
checkBox6.setFocusable(false);
checkBox7.setFocusable(false);
checkBox8.setFocusable(false);
submit = new JButton();
submit.setText("submit");
submit.addActionListener(this);
this.add(submit);
submit.setFocusable(false);
this.revalidate();
this.repaint();
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==submit)
{
System.out.println(checkBox0.isSelected());
System.out.println(checkBox1.isSelected());
System.out.println(checkBox2.isSelected());
System.out.println(checkBox3.isSelected());
System.out.println(checkBox4.isSelected());
System.out.println(checkBox5.isSelected());
System.out.println(checkBox6.isSelected());
System.out.println(checkBox7.isSelected());
System.out.println(checkBox8.isSelected());
System.out.println(area.getText());
System.out.println(level.getText());
System.out.println(hours.getText());
System.out.println(workType);
if(checkBox5.isSelected())
{
workType = "chop";
this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
}
else if(checkBox6.isSelected())
{
workType = "fish";
this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
}
else if(checkBox7.isSelected())
{
workType = "pickup";
this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
}
else if(checkBox7.isSelected())
{
workType = "mine";
this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
}
else
{
workType = "none";
this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
}
areaOne = area.getText();
levelOne = level.getText();
hoursToRun = hours.getText();
playerArea = Integer.parseInt(areaOne);
playerLevel = Integer.parseInt(levelOne);
hoursRunning = Integer.parseInt(hoursToRun);
Working working = new Working(playerLevel, playerArea, workType);
}
}
}
package rpg;
import java.util.Scanner;
public class Working
{
//chop
//pickup
//mine
//fish
Scanner input;
int area, level;
String typeOfWork;
public void startUp()
{
System.out.println("PTINR ME");
}
public Working(int level, int area, String typeOfWork)
{
int playerLevel = level;
int playerArea = area;
String job = typeOfWork;
}
}
暂无答案!
目前还没有任何答案,快来回答吧!