我的JFrame中的另一个重新绘制问题

时间:2017-07-17 15:14:22

标签: java swing jpanel repaint

我正在学习Java并测试一个简单的笔记记录程序。我有一个在启动时隐藏的面板,只需单击一个按钮即可看到。但是在我调整程序窗口之前我无法看到这个面板!我真诚地尝试了我在网上和那里找到的所有东西,但仍然无法让它正常运行。这是代码:

//Main JFrame "Fenetre"
//imports
...

public class Fenetre extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;
    private JPanel west = new JPanel();
    private CEPan CEPan = new CEPan();
    private JPanel container = new JPanel();
    private JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, west, CEPan);
    private JButton nouvelleNote = new JButton("Nouvelle note");

    public Fenetre() {
        this.setMinimumSize(new Dimension(800, 700));
        this.setTitle("Notes");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setContentPane(container);
        container.setBackground(Color.GREEN);
        west.setMinimumSize(new Dimension(100, 700));
        west.add(nouvelleNote);
        nouvelleNote.addActionListener(this);
        CEPan.setMinimumSize(new Dimension(200, 700));
        CEPan.setBackground(Color.RED);

        split.setDividerSize(4);
        container.setLayout(new BorderLayout());
        container.add(split, BorderLayout.CENTER);

        //At first this panel is hidden until the click of the button
        CEPan.setVisible(false);

        this.setContentPane(container);
    }


    public void nouvelleNote() {
        CEPan.setVisible(true);
        container.repaint();
        west.repaint();
        this.repaint();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        nouvelleNote();
    }

}

CEPan

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class CEPan extends JPanel {

    private static final long serialVersionUID = 1L;
    private TextArea titleTF = new TextArea(35);
    private TextArea contentTA = new TextArea(16);
    private JScrollPane scroll = new JScrollPane(contentTA, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

    private JScrollPane scrollTitle = new JScrollPane(titleTF);
    public boolean hide;

    public CEPan() {        
        titleTF.setBackground(Color.BLUE);
        this.setLayout(new BorderLayout());
        this.add(scrollTitle, BorderLayout.NORTH);
        this.add(scroll, BorderLayout.CENTER);
        titleTF.setEditable(true);

        contentTA.setLineWrap(true);
        scrollTitle.setBackground(Color.GRAY);
    }
}

TextArea

public class TextArea extends JTextArea {

    private static final long serialVersionUID = 1L;
    private Color couleur = new Color(1, 168, 135);

    public TextArea(int fs) {
        Font font = new Font("Arial", Font.BOLD, fs);
        this.setFont(font);
        this.setForeground(couleur);
        //this.setPreferredSize(new Dimension(40, 40));
    }
}

Notes

public class Notes {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Fenetre fenetre = new Fenetre();
        fenetre.pack();
        fenetre.setVisible(true);
    }
}

3 个答案:

答案 0 :(得分:0)

当已经显示<div class='container'> <div class='flex-container'> <div class='flex-overlapping-item'> <h3>Drag photo here</h3> <p>Photo must have 1000px x 1000px</p> </div> <div class='flex-overlapping-item drag-zone'> <div class='drag-zone-content'> </div> </div> </div> </div>且某个事件导致修改此类JCompoment时,避免出现问题的最佳方法是始终在所涉及的组件上调用JComponentrepaint()。第一种方法导致组件重新绘制以显示涉及组件外观的任何更改(例如更改颜色),第二种方法通知布局管理器重新计算布局,因为如果添加或删除了某些组件,则必须

答案 1 :(得分:0)

resize(900,700)可见之后,只需添加一行代码:CEPan

`public void nouvelleNote() {
   CEPan.setVisible(true);
   resize(900, 700);
 }`

这会调整JFrame的大小,然后它会自动重新绘制并重新验证所有组件。

答案 2 :(得分:-2)

I've had this problem before. All you need to replace all repaint method calls with:

yourJFrame.setVisible(true);

Eg.:

public void nouvelleNote() {
    CEPan.setVisible(true);
    this.setVisible(true);
}

Even if the frame is already visible this method will reassure that everything is rendered correctly.