[英]Can't change the output of paintComponent after assigned new variable (setter) and called repaint()
我被困在同一個地方將近 1 天,但我仍然找不到我的代碼錯誤在哪里。 需要你們的幫助,這是我的代碼。 (有點長)
我的 JFrame class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CircleProject extends JFrame implements ActionListener{
JTextField xTextField1;
JTextField yTextField1;
JTextField textFieldRadiusLeft;
JTextField xTextField2;
JTextField yTextField2;
JTextField textFieldRadiusRight;
JButton redraw;
public static void main(String[] args) {
CircleProject frame = new CircleProject();
frame.setResizable(true);
frame.setVisible(true);
frame.setSize(580, 700);
frame.setTitle("YONG JING PING FINAL PROJECT");
}
public CircleProject() {
JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JLabel theTitle = new JLabel("Two Circles Intersect?");
JLabel intersection = new JLabel("");
titlePanel.add(theTitle);
titlePanel.add(intersection);
JPanel contentPanel = new JPanel(new GridLayout(2, 1));
JPanel userInputPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JPanel gridPanel = new JPanel(new GridLayout(1, 2, 8, 0));
JPanel leftInputPanel = new JPanel(new GridLayout(4, 1));
JLabel inputTitle1 = new JLabel("Enter circle 1 info: ");
inputTitle1.setHorizontalAlignment(JLabel.CENTER);
JPanel xInputPanelLeft = new JPanel();
xTextField1 = new JTextField(5);
xInputPanelLeft.add(new JLabel("Center X: "));
xInputPanelLeft.add(xTextField1);
JPanel yInputPanelLeft = new JPanel();
yTextField1 = new JTextField(5);
yInputPanelLeft.add(new JLabel("Center Y: "));
yInputPanelLeft.add(yTextField1);
JPanel radiusLeft = new JPanel();
textFieldRadiusLeft = new JTextField(5);
radiusLeft.add(new JLabel("Radius: "));
radiusLeft.add(textFieldRadiusLeft);
leftInputPanel.add(inputTitle1);
leftInputPanel.add(xInputPanelLeft);
leftInputPanel.add(yInputPanelLeft);
leftInputPanel.add(radiusLeft);
leftInputPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
JPanel rightInputPanel = new JPanel(new GridLayout(4, 1));
JLabel inputTitle2 = new JLabel("Enter circle 2 info: ");
inputTitle2.setHorizontalAlignment(JLabel.CENTER);
JPanel xInputPanelRight = new JPanel();
xTextField2 = new JTextField(5);
xInputPanelRight.add(new JLabel("Center X: "));
xInputPanelRight.add(xTextField2);
JPanel yInputPanelRight = new JPanel();
yTextField2 = new JTextField(5);
yInputPanelRight.add(new JLabel("Center Y: "));
yInputPanelRight.add(yTextField2);
JPanel radiusRight = new JPanel();
textFieldRadiusRight = new JTextField(5);
radiusRight.add(new JLabel("Radius: "));
radiusRight.add(textFieldRadiusRight);
rightInputPanel.add(inputTitle2);
rightInputPanel.add(xInputPanelRight);
rightInputPanel.add(yInputPanelRight);
rightInputPanel.add(radiusRight);
rightInputPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
gridPanel.add(leftInputPanel);
gridPanel.add(rightInputPanel);
userInputPanel.add(gridPanel);
contentPanel.add(new DrawPanel());
contentPanel.add(userInputPanel);
redraw = new JButton("Redraw Circles");
redraw.setEnabled(true);
redraw.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.add(redraw);
setLayout(new BorderLayout());
add(titlePanel, BorderLayout.NORTH);
add(contentPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
}
DrawPanel drawCircle = new DrawPanel();
@Override
public void actionPerformed(ActionEvent e) {
this.setVisible(false);
if (e.getSource() == redraw) {
CircleProject frame2 = new CircleProject();
frame2.setResizable(false);
frame2.setVisible(true);
frame2.setSize(580,700);
frame2.setTitle("YONG JING PING FINAL PROJECT");
String text = xTextField1.getText();
int x1 = Integer.parseInt(text);
String text1 = xTextField2.getText();
int x2 = Integer.parseInt(text1);
String text2 = yTextField1.getText();
int y1 = Integer.parseInt(text2);
String text3 = yTextField2.getText();
int y2 = Integer.parseInt(text3);
String text4 = textFieldRadiusLeft.getText();
int r1 = Integer.parseInt(text4);
String text5 = textFieldRadiusRight.getText();
int r2 = Integer.parseInt(text5);
drawCircle.setCenterColumn1(x1, y1, r1);
drawCircle.setCenterColumn2(x2, y2, r2);
}
}
}
我的繪圖板 Class:
import javax.swing.*;
import java.awt.*;
public class DrawPanel extends JPanel{
// the coordinate's variables is the start point of it.
int coordinateX1=50, coordinateX2=300, coordinateY1=50, coordinateY2=50, radius1=0, radius2=0, length1=200, length2=200;
public void setCenterColumn1(int x, int y, int radius1){
this.coordinateX1 = x-radius1;
this.coordinateY1 = y-radius1;
this.radius1 = radius1;
length1 = radius1*2;
System.out.println(coordinateX1);
System.out.println(coordinateY1);
System.out.println(length1);
System.out.println(coordinateX2);
System.out.println(coordinateY2);
System.out.println(length2);
System.out.println("-------1-------");
repaint();
}
public void setCenterColumn2(int x, int y, int radius2){
this.coordinateX2=x-radius2;
this.coordinateY2=y-radius2;
this.radius2=radius2;
length2 = radius2*2;
System.out.println(coordinateX1);
System.out.println(coordinateY1);
System.out.println(length1);
System.out.println(coordinateX2);
System.out.println(coordinateY2);
System.out.println(length2);
System.out.println("-------2-------");
repaint();
}
public DrawPanel(){
setBackground(Color.WHITE);
}
// error from here, when redraw pressed this paintComponent does not
// show up the new circle.
@Override
public void paintComponent (Graphics g){
super.paintComponent(g);
g.setColor(Color.BLACK);
// Circle for column 1
g.drawOval(coordinateX1,coordinateY1,length1,length1);
// Circle for column 2
g.drawOval(coordinateX2,coordinateY2,length2,length2);
System.out.println(coordinateX1);
System.out.println(coordinateY1);
System.out.println(length1);
System.out.println(coordinateX2);
System.out.println(coordinateY2);
System.out.println(length2);
System.out.println("-------3-------");
}
}
我的預期結果是用戶輸入圓的中心坐標並單擊重繪 btn 后,圓將發生變化。
問題是你沒有使用你的變量drawCircle
(line: DrawPanel drawCircle = new DrawPanel();)
當您使用內部actionPerformed
方法時,您永遠不會使用該變量,因為它創建了 Circle Project 的另一個實例。 (行:CircleProject frame2 = new CircleProject();)
drawCircle
變量來自您正在運行的當前 class,因此,當您再次實例化 CircleProject 時,該變量內部的更改將不起作用。
要解決此問題,無需再次實例化,您只需使用該變量即可。
您必須在構造函數CircleProject()
中做的第一件事:
....
userInputPanel.add(gridPanel);
contentPanel.add(drawCircle);
contentPanel.add(userInputPanel);
....
這里的變化只有一個,因為你有
contentPanel.add(new DrawPanel());
這是不正確的(正如我所說,您從未使用過變量drawCircle
),因此,您必須將其更改為contentPanel.add(drawCircle);
另一方面,為避免實例化CircleProject
,您必須刪除actionPerformed
方法中的一些行,例如:
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == redraw) {
String text = xTextField1.getText();
int x1 = Integer.parseInt(text);
String text1 = xTextField2.getText();
int x2 = Integer.parseInt(text1);
String text2 = yTextField1.getText();
int y1 = Integer.parseInt(text2);
String text3 = yTextField2.getText();
int y2 = Integer.parseInt(text3);
String text4 = textFieldRadiusLeft.getText();
int r1 = Integer.parseInt(text4);
String text5 = textFieldRadiusRight.getText();
int r2 = Integer.parseInt(text5);
drawCircle.setCenterColumn1(x1, y1, r1);
drawCircle.setCenterColumn2(x2, y2, r2);
}
}
我試過這個程序,它對我有用:我希望這也對你有用:D
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.