【发布时间】:2017-07-17 15:14:21
【问题描述】:
我正在学习 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);
}
}
【问题讨论】:
-
public class TextArea extends JTextArea {这是一个令人困惑的类名称,因为有一个java.awt.TextArea。此外,工厂方法会更好地生成文本区域。 -
仅在根对象上调用
repaint()将重绘其所有子对象,因此nouvelleNote()应该只调用this.repaint()甚至更好的是repaint(),因为隐含了this -
“我有一个在启动时隐藏的面板,通过单击按钮使其可见。” 使用
CardLayout,如this answer 所示。 -
-
感谢所有有用的 cmets。事实证明,问题并不完全在于调整窗口大小。只有当我调整窗口右侧的大小时才可以,更准确地说是 CEPan。直到 Divider 被拖动一毫米,CEPan 才出现。我仍然不明白这个问题,但我只能通过在单击按钮时实例化 JSplitPanel 来修补它,否则我只有带有按钮的“西”面板