문제

이것은 힘든 것입니다 - 나는 jtextFields를 생성하는 JFrame을 가지고 있습니다. 2 개의 jtextfields를 생성하여 12 jtextFields (예 : 12 개의 jtextFields)로 이동하면 마지막에 다른 크기의 JtextField가있는 오류가 나타납니다. 레페인트 오류 인 것 같습니다.

Main.java 코드 :

import java.awt.*;
import javax.swing.*;

public class Main {
    public static Display display = new Display();

    public static void main(String[] args) {
        display.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        display.setVisible(true);
    }
}

display.java 코드 :

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Display extends JFrame {
    final int FRAME_WIDTH = 820;
    final int FRAME_HEIGHT = 700;
    final int X_OFFSET = 40;
    final int Y_OFFSET = 40;

    final int GRAPH_OFFSETX = 35;
    final int GRAPH_OFFSETY = 60;
    final int GRAPH_WIDTH = 500;
    final int GRAPH_HEIGHT = 500;
    final int GRAPH_INTERVAL = 20;

    JButton submit;
    JTextField top;
    JTextField bottom;
    JTextField numPoint;
    JPanel bpanel;
    JPanel points;

    int maxPoints;

    public Display() {
        init();
    }

    public void init() {
        setBackground(Color.WHITE);
        setLocation(X_OFFSET, Y_OFFSET);
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        setTitle("Geometric Transformations");
        getContentPane().setLayout(null);
        setDefaultLookAndFeelDecorated(true);

        top = new JTextField();    // parameter is size of input characters
        top.setText("1 2 3");
        top.setBounds(590, 150, 120, 25);

        bottom = new JTextField();    // parameter is size of input characters
        bottom.setText("5 6 7");
        bottom.setBounds(590, 200, 120, 25);

        numPoint = new JTextField();
        numPoint.setText("Number of Points?");
        numPoint.setBounds(550,200,200,25);
        this.add(numPoint);

        SubmitButton submit = new SubmitButton("Submit");
        submit.setBounds(570, 250, 170, 25);

        bpanel = new JPanel(new GridLayout(2,3));
        bpanel.add(top);
        bpanel.add(bottom);
        bpanel.add(submit);

        points = new JPanel(new GridLayout(2,2));
        points.setBounds(540,250,265,60);
        this.add(points);

        bpanel.setBounds(550,100,200,70);
        this.add(bpanel, BorderLayout.LINE_START);

        Component[] a = points.getComponents();
        System.out.println(a.length);
        repaint();
    }

    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(Color.WHITE);
        g.fillRect(100, 100, 20, 30);
        g.setColor(Color.BLACK);
        genGraph(g, GRAPH_OFFSETX, GRAPH_OFFSETY, GRAPH_WIDTH, GRAPH_HEIGHT, GRAPH_INTERVAL);
    }

    public void genGraph (Graphics g, int x, int y, int width, int height, int interval) {
        // draw background
        int border = 5;
        g.setColor(Color.BLACK);
        width = width - (width % interval);
        height = height - (height % interval);
        for (int col=x; col <= x+width; col+=interval) {
            g.drawLine(col, y, col, y+height);
        }
        for (int row=y; row <= y+height; row+=interval) {
            g.drawLine(x, row, x+width, row);
        }
    }
    class SubmitButton extends JButton implements ActionListener {

        public SubmitButton(String title){
            super(title);
            addActionListener(this);
            this.setVisible(true);
        }
        public void actionPerformed(ActionEvent e) {
            maxPoints = Integer.parseInt(numPoint.getText()) * 2;

            points.removeAll();

            for (int i=0; i<maxPoints; i++) {
                JTextField textField = new JTextField();
                points.add(textField);
            }
            points.validate();        // necessary when adding components to a JPanel
            // http://stackoverflow.com/questions/369823/java-gui-repaint-problem-solved
            // What to Check:
            // Things between commas are either spaces (which will be stripped later)
            // or numbers!

            // Pairs must match up!
        }
    }
}
도움이 되었습니까?

해결책

새로운 구성 요소는 이전 구성 요소를 통해 다시 그려집니다. 나는 추가했다 points.repaint(); ~ 후에 points.validate(); 그리고 문제는 사라졌습니다.

다른 팁

참고 : 나는 그리드의 페인팅 문제에 대해 화가났다 (창을 앞에두면 다시 보게 될 것이다).
빠른 검색에서 JFrame에서 직접 그림을 피우는 것을 피하는 것이 좋습니다. 대신 하위 구성 요소에이를 위임하는 것이 좋습니다. 내가 틀렸다면 누군가 나에게 말해줘. 여기 내 해결책이 있습니다 (불완전한, 나는 그것을 개선하는 과제를 당신에게 맡깁니다 ... : -p.

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class SODisplay extends JFrame {
    final int FRAME_WIDTH = 820;
    final int FRAME_HEIGHT = 700;
    final int X_OFFSET = 40;
    final int Y_OFFSET = 40;

    JButton submit;
    JTextField top;
    JTextField bottom;
    JTextField numPoint;
    JPanel bpanel;
    JPanel points;
    GridPanel grid;

    int maxPoints;

    public SODisplay() {
        init();
    }

    public void init() {
        setBackground(Color.WHITE);
        setLocation(X_OFFSET, Y_OFFSET);
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        setTitle("Geometric Transformations");
        getContentPane().setLayout(null);
        setDefaultLookAndFeelDecorated(true);

        grid = new GridPanel();
        grid.setBounds(0,0,530,FRAME_HEIGHT);
        this.add(grid);

        top = new JTextField();    // parameter is size of input characters
        top.setText("1 2 3");
        top.setBounds(590, 150, 120, 25);

        bottom = new JTextField();    // parameter is size of input characters
        bottom.setText("5 6 7");
        bottom.setBounds(590, 200, 120, 25);

        numPoint = new JTextField();
        numPoint.setText("Number of Points?");
        numPoint.setBounds(550,200,200,25);
        this.add(numPoint);

        SubmitButton submit = new SubmitButton("Submit");
        submit.setBounds(570, 250, 170, 25);

        bpanel = new JPanel(new GridLayout(2,3));
        bpanel.add(top);
        bpanel.add(bottom);
        bpanel.add(submit);

        points = new JPanel(new GridLayout(2,2));
        points.setBounds(540,250,265,60);
        this.add(points);

        bpanel.setBounds(550,100,200,70);
        this.add(bpanel, BorderLayout.LINE_START);

        Component[] a = points.getComponents();
        System.out.println(a.length);
        repaint();
    }

    class SubmitButton extends JButton implements ActionListener {

        public SubmitButton(String title){
            super(title);
            addActionListener(this);
            this.setVisible(true);
        }
        public void actionPerformed(ActionEvent e) {
            maxPoints = Integer.parseInt(numPoint.getText()) * 2;

            points.removeAll();

            for (int i=0; i<maxPoints; i++) {
                JTextField textField = new JTextField();
                points.add(textField);
            }
            points.validate();        // necessary when adding components to a JPanel
            points.repaint();
            // http://stackoverflow.com/questions/369823/java-gui-repaint-problem-solved
            // What to Check:
            // Things between commas are either spaces (which will be stripped later)
            // or numbers!

            // Pairs must match up!
        }
    }

    public static void main(String[] args) {
        // Schedule a job for the event-dispatching thread:
        // creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                SODisplay display = new SODisplay();
                display.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                display.setVisible(true);
            }
        });
    }

    class GridPanel extends JPanel {
        // Or drop the offset and adjust the placement of the component
        final int GRAPH_OFFSETX = 35;
        final int GRAPH_OFFSETY = 60;
        final int GRAPH_WIDTH = 500;
        final int GRAPH_HEIGHT = 500;
        final int GRAPH_INTERVAL = 20;

        public GridPanel() {
        }

        public void paint(Graphics g) {
            super.paint(g);
            g.setColor(Color.WHITE);
            g.fillRect(100, 100, 20, 30);
            g.setColor(Color.BLACK);
            genGraph(g, GRAPH_OFFSETX, GRAPH_OFFSETY, GRAPH_WIDTH, GRAPH_HEIGHT, GRAPH_INTERVAL);
        }

        public void genGraph (Graphics g, int x, int y, int width, int height, int interval) {
            // draw background
            int border = 5;
            g.setColor(Color.BLACK);
            width = width - (width % interval);
            height = height - (height % interval);
            for (int col=x; col <= x+width; col+=interval) {
                g.drawLine(col, y, col, y+height);
            }
            for (int row=y; row <= y+height; row+=interval) {
                g.drawLine(x, row, x+width, row);
            }
        }
    }
}

단순성을 위해 하나의 파일의 테스트 코드 일뿐입니다. 물론 다르게 해부 할 수 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top