我很清楚我不会使用null布局。但是,目前我用它来测试一些东西。我的程序很简单:按墨西哥按钮显示Diplo按钮。问题是当我将鼠标滚动到显示后的Diplo按钮上时,mex按钮文本出现在它上面。我现在假设我的问题不知何故涉及null布局,不使用paintcomponent,或者使用方法,但我不确定它可能会出错。
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class PanelTest extends JPanel
{
//Country Lists
public Country Mex = new Country("Mexico","Empire",15);
public Country Sel;
//Country buttons
public JButton buttonMex;
//Foreign Choices
private JButton buttonAggro;
public JButton buttonDip;
private JButton buttonEspo;
public PanelTest(){
setLayout(null);
//Definitions for button
buttonDip = new JButton();
buttonDip.setForeground(Color.BLACK);
buttonDip.setFont(new Font("Serif", Font.BOLD, 20));
buttonDip.setText("Diplo");
buttonDip.addActionListener(new DipListener());
buttonDip.setSize(100, 50);
buttonDip.setLocation(0,100);
buttonDip.setBorder(null);
buttonDip.setOpaque(false);
buttonDip.setContentAreaFilled(false);
buttonDip.setBorderPainted(false);
buttonDip.setFocusPainted(false);
add(buttonDip);
buttonDip.setEnabled(false);
buttonDip.setVisible(false);
buttonMex = new JButton();
buttonMex.setForeground(Color.BLACK);
buttonMex.setFont(new Font("Serif", Font.BOLD, 20));
buttonMex.setText("Mexico");
buttonMex.addActionListener(new MexListener());
buttonMex.setSize(100, 50);
buttonMex.setLocation(0,0);
buttonMex.setBorder(null);
buttonMex.setOpaque(false);
buttonMex.setContentAreaFilled(false);
buttonMex.setBorderPainted(false);
buttonMex.setFocusPainted(false);
add(buttonMex);
buttonMex.setEnabled(false);
buttonMex.setVisible(false);
//MAIN
buttonValid();
}
public void paintComponent(Graphics g)
{
}
public void buttonValid(){
if(Mex.getVal() == true){
buttonMex.setEnabled(true);
buttonMex.setVisible(true);
}
}
public void FTest()
{
buttonMex.setEnabled(false);
buttonMex.setVisible(false);
buttonDip.setEnabled(true);
buttonDip.setVisible(true);
}
private class MexListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
FTest();
Sel = Mex;
}
}
private class DipListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Mex.setRel(-30);
}
}
这是我的节目制作的照片
答案 0 :(得分:1)
public void paintComponent(Graphics g)
{
}
不要覆盖paintComponent()。没有理由拥有一个空方法。摆脱那段代码:
当您执行此操作时,面板的背景将永远不会被清除,并且您可以拥有绘画工件。