尝试代码简单gui。学习如何为每个gui元素使用单独的类。 这是简单的代码。 头等舱
public class FieldDraw extends JPanel {
JPanel field;
public FieldDraw() {
field = new JPanel();
field.setBorder(new LineBorder(new Color(0, 0, 0)));
}
public void addField(JPanel p) {
p.add(field, BorderLayout.CENTER);
}
public void drawRect(Graphics g, int start) {
super.paintComponent(g);
g.drawString("BLAH", 100, 100);
g.drawRect(start, 200, 200, 200);
}
public void clean() {
// TODO Auto-generated method stub
field.repaint();
}
}
第二
public class Controlls extends JPanel implements ChangeListener {
JPanel controls;
JButton btn1;
JButton btn2;
JSlider fieldResolution;
int test;
public Controlls() {
controls = new JPanel();
btn1 = new JButton("weqwe");
btn1.setAlignmentX(Component.RIGHT_ALIGNMENT);
btn1.setMaximumSize(new Dimension(Integer.MAX_VALUE, btn1.getMinimumSize().height));
btn2 = new JButton("weqwefdaf");
btn2.setAlignmentX(Component.RIGHT_ALIGNMENT);
btn2.setMaximumSize(new Dimension(Integer.MAX_VALUE, btn2.getMinimumSize().height));
Component vert5 = Box.createVerticalStrut(5);
Component vert20 = Box.createVerticalStrut(20);
fieldResolution = new JSlider(JSlider.HORIZONTAL, FIELD_SIZE_MIN, FIELD_SIZE_MAX, FIELD_SIZE_MAX / 4);
fieldResolution.setMajorTickSpacing(100);
fieldResolution.setPaintTicks(true);
fieldResolution.setAlignmentX(Component.RIGHT_ALIGNMENT);
fieldResolution.addChangeListener(this);
Hashtable<Integer, JLabel> resolutionLabel = new Hashtable<Integer, JLabel>();
resolutionLabel.put(FIELD_SIZE_MIN, new JLabel(String.valueOf(FIELD_SIZE_MIN)));
resolutionLabel.put(FIELD_SIZE_MAX / 2, new JLabel(String.valueOf(FIELD_SIZE_MAX / 2)));
resolutionLabel.put(FIELD_SIZE_MAX, new JLabel(String.valueOf(FIELD_SIZE_MAX)));
fieldResolution.setLabelTable(resolutionLabel);
fieldResolution.setPaintLabels(true);
fieldResolution.setMaximumSize(new Dimension(Integer.MAX_VALUE, fieldResolution.getMinimumSize().height));
//fieldResolution.setma
controls.setLayout(new BoxLayout(controls, BoxLayout.Y_AXIS));
controls.add(btn1);
controls.add(vert5);
controls.add(btn2);
controls.add(vert20);
controls.add(fieldResolution);
}
public void addControlls(JPanel p) {
p.add(controls, BorderLayout.EAST);
}
public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
System.out.println(fieldResolution.getValue());
}
}
第三
public class Gui extends JFrame {
JPanel guiFrame;
FieldDraw field;
Controlls controlls;
public Gui() {
guiFrame = new JPanel();
field = new FieldDraw();
controlls = new Controlls();
setTitle("Main window"); // Название окна
setBounds(0, 0, 300, 300); // Если не выставить
// размер и положение
// то окно будет мелкое и незаметное
// setUndecorated(true); // убирает рамку окна(крестик, развернуть и тд.
guiFrame.setBorder(new EmptyBorder(5, 5, 5, 5));
guiFrame.setLayout(new BorderLayout());
setContentPane(guiFrame);
field.addField(guiFrame);
controlls.addControlls(guiFrame);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
controlls.fieldResolution.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
// field.field.update(getGraphics());
field.drawRect(getGraphics(), controlls.fieldResolution.getValue());
// field.update(getGraphics());
// repaint();
}
});
}
}
一个类是用于绘制内容的空字段,第二类是一些控件,第三类是Jframe类。我放滑块,如果您将其移动,则该字段上的正方形应移动。是的。但它保留了所有先前的图纸 screenshot
这是问题。 repaint()不更新jpanel。一切都很干净。我可以看到何时移动滑块。它只是眨眼一秒钟,然后将所有内容全部清除。
答案 0 :(得分:-2)
我以前有这个问题。 尝试使用.pack。关于此的文档还不清楚,但我为我工作了