여기에서 비슷한 문제를 읽었지 만 여전히 코드 수정 방법을 알 수 없습니다. 나는 타이머를 테스트 중이며 gui가 별도의 스레드에서 열리는 동안 타이머 출력을 명령 줄에 넣을 수 있지만 attarea라는 JtextArea에 타이머 출력을 가져올 수 없습니다. 문제를 일으키는 Gui 클래스의 인스턴스를 참조하지 않고 있지만 내 코드를 수정하는 방법을 모르겠습니다. Gui 클래스에 속한 buildGui 메서드 내에서 내 타이머 클래스를 인스턴스화하려고했습니다. 그런 식으로 컴파일하고 타이머가 실행되고 있기 때문에 GUI에서 종료하면 나에게 명령 줄에 새로운 프롬프트가 표시되지 않으므로 타이머가 자체 스레드에서 실행 중이어야합니다. 그러나 attarea에는 인쇄되지 않습니다. 아래에있는 내 코드 (3 개 클래스)정적 컨텍스트에서 비 정적 변수를 참조 할 수 없습니다.
class Main{
public static void main(String[] args){
new Gui();
new TimerTest();
}
}
다음 수업
import javax.swing.*;
import java.awt.*;
class Gui extends JFrame{
int fieldWidth = 20;
String[] choose = {"Choose"};
JPanel pnlBack = new JPanel();
JPanel topPanel = new JPanel();
JPanel citiesPanel = new JPanel();
JLabel citiesLabel = new JLabel("Cities: ");
JComboBox citiesBox = new JComboBox(choose);
//citiesBox.add("choose");
JPanel typePanel = new JPanel();
JLabel typeLabel = new JLabel("Type: ");
JComboBox typeBox = new JComboBox(choose);
JPanel namePanel = new JPanel();
JLabel nameLabel = new JLabel("Name: ");
JComboBox nameBox = new JComboBox(choose);
JPanel midPanel = new JPanel();
JPanel attPanel = new JPanel();
JTextArea attArea = new JTextArea(12, 50);
JPanel bottomPanel = new JPanel();
JPanel gasPricePanel = new JPanel();
JLabel gasPriceLabel = new JLabel(" Price of gas: ");
JTextField gasPriceBox = new JTextField(fieldWidth);
JPanel mpgPanel = new JPanel();
JLabel mpgLabel = new JLabel(" mpg of vehicle: ");
JTextField mpgBox = new JTextField(fieldWidth);
JPanel moneyPanel = new JPanel();
JLabel moneyLabel = new JLabel(" Money you have:");
JTextField moneyBox = new JTextField(fieldWidth);
JPanel costPanel = new JPanel();
JLabel costLabel = new JLabel(" Cost of trip: ");
JTextField costBox = new JTextField(fieldWidth);
JPanel distancePanel = new JPanel();
JLabel distanceLabel = new JLabel(" Distance you can go: ");
JTextField distanceBox = new JTextField(fieldWidth);
Gui(){
buildGui();
}
//method to buld the gui
void buildGui(){
this.setSize(600,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Travel Calc");
this.setResizable(false);
this.setVisible(true);
//System.out.println("buiding the gui.");
pnlBack.setLayout(new BoxLayout(pnlBack, BoxLayout.Y_AXIS));
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
topPanel.setBorder(BorderFactory.createLineBorder(Color.black));
midPanel.setLayout(new BoxLayout(midPanel, BoxLayout.Y_AXIS));
midPanel.setBorder(BorderFactory.createLineBorder(Color.black));
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.Y_AXIS));
bottomPanel.setBorder(BorderFactory.createLineBorder(Color.black));
//add back panel to Jframe
add(pnlBack);
//add panels to back panel
pnlBack.add(topPanel);
topPanel.add(citiesPanel);
citiesPanel.add(citiesLabel);
citiesPanel.add(citiesBox);
topPanel.add(typePanel);
typePanel.add(typeLabel);
typePanel.add(typeBox);
topPanel.add(namePanel);
namePanel.add(nameLabel);
namePanel.add(nameBox);
pnlBack.add(midPanel);
midPanel.add(attPanel);
attPanel.add(attArea);
pnlBack.add(bottomPanel);
bottomPanel.add(gasPricePanel);
gasPricePanel.add(gasPriceLabel);
gasPricePanel.add(gasPriceBox);
bottomPanel.add(mpgPanel);
mpgPanel.add(mpgLabel);
mpgPanel.add(mpgBox);
bottomPanel.add(moneyPanel);
moneyPanel.add(moneyLabel);
moneyPanel.add(moneyBox);
bottomPanel.add(costPanel);
costPanel.add(costLabel);
costPanel.add(costBox);
costBox.setEditable(false);
bottomPanel.add(distancePanel);
distancePanel.add(distanceLabel);
distancePanel.add(distanceBox);
distanceBox.setEditable(false);
// add connection method goes here
// new TimerTest();
}
}
3 종
import java.util.Timer;
import java.util.TimerTask;
public class TimerTest {
static int counter = 0;
public TimerTest(){
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
Gui.attArea.append("TimerTask executing counter is: " + counter);
//System.out.println("TimerTask executing counter is: " + counter);
counter++;//increments the counter
}
};
Timer timer = new Timer("MyTimer");//create a new Timer
timer.scheduleAtFixedRate(timerTask, 0, 3000);//this line starts the timer at the same time its executed
}
}