在我的代码中,我将JPanel
(Bestellpanel)从frame
转移到frame1
。之后,每当我使用frame1
滚动条时,它重新绘制frame1
,而JPanel
(Bestellpanel)就会消失。这意味着我需要一种方法来阻止我的JPanel
过度涂抹。我读了一些关于super.paint();
和其他方法的内容,但我对它们有很大的理解。
以下是我的问题的代码示例:
import java.awt.Color;
import javax.swing.JPanel;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
public class weqe {
private static JFrame frame = new JFrame("First Frame");
private static JFrame frame1 = new JFrame("Second Frame");
private static JPanel Bestellpanel = new JPanel();
private static int kunde = 1;
public static void addComponentsToPane(final Container pane) {
pane.setLayout(null);
final Insets insets1 = pane.getInsets();
// Mitn Button
JButton MitnIcon = new JButton("Mitnehmen");
MitnIcon.setFocusPainted(false);
MitnIcon.setVisible(true);
Dimension size2 = MitnIcon.getPreferredSize();
MitnIcon.setBounds(1010 + insets1.left, 700 + insets1.top,
size2.width + 27, size2.height + 50);
pane.add(MitnIcon);
MitnIcon.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (kunde == 1) {
frame.getContentPane().remove(Bestellpanel);
Bestellpanel.setLocation(0, 0);
frame1.getContentPane().add(Bestellpanel);
Bestellpanel.repaint();
frame.repaint();
}
}});
// ScrollPane
JPanel panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(2000,800));
panel1.setVisible(false);
JScrollPane scrollPane = new JScrollPane (panel1,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
frame1.add(scrollPane);
Bestellpanel.setBounds(930 + insets1.left, 50 + insets1.top,size2.width
+ 30, size2.height + 400);
Bestellpanel.setVisible(true);pane.add(Bestellpanel);
Bestellpanel.setBackground(Color.green);
}
private static void createAndShowGUI() {
//Create and set up the window.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
//Size and display the window.
Insets insets = frame.getInsets();
Insets insets1 = frame1.getInsets();
frame.setSize(1200 + insets.left + insets.right,
900 + insets.top + insets.bottom);
frame.setVisible(true);
frame1.setSize(800 + insets1.left + insets1.right,
600 + insets1.top + insets1.bottom);
frame1.setVisible(true);
frame.add(Bestellpanel);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
答案 0 :(得分:2)
meinJDialog.setSize(800,800);
和panel1.setPreferredSize(new Dimension(2000,800));
很可能是您问题的一部分,请参阅Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?(普遍共识是肯定并改为覆盖getPreferred|Maximum|MinimumSize()
方法)
不要自行删除/添加JComponent
,请尝试Card Layout
您不需要手动更改组件的可见性,请再次检查第2点的链接:Bestellpanel2.setVisible(true);
请关注Java naming conventions:FirstWordUpperCaseClass
,firstWordLowerCaseVariable
,firstWordLowerCaseMethod()
和ALL_WORDS_UPPER_CASE_CONSTANT
),因此,您的代码更易于阅读和理解为了你和我们。
如果上述所有要点都不起作用,请考虑发布有效的Minimal, Complete and Verifiable Example (MCVE)或Short, Self Contained, Correct Example (SSCCE)来证明您的问题,没有外部依赖关系或自定义设置,例如背景颜色/图片等它应该正确缩进,如上面的评论所述。