我在事件监听中修改一个JLabel里的文字,但是原来的字体没有清除,后面的文字又叠加上去,如果不在监听中修改就正常,求助要怎么解决,是什么的原因?

解决方案 »

  1.   

    估计你实例化了两个JLabel对象。
    还是发代码吧。要么谁也不能判断出错误原因。
      

  2.   

    class test{
    public static void main(String[] args)throws Exception{
    MyFrame f = new MyFrame();
    f.setSize(500,300);
    f.setVisible(true);
    }//main
    }
             
    class MyFrame extends JFrame {
    public MyFrame()throws Exception{
       MainPanel mainPanel = new MainPanel();
       setLayout(new BorderLayout());
       add(mainPanel,"Center");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    }
    class MainPanel extends JPanel{
    // Declare a label for displaying message
    private JLabel jLabel1 = new JLabel("Display the border type",JLabel.CENTER);

    //A check box for seleting a border with or without a title
    private JCheckBox jchkTitled;

    // Radio buttons for border styles
    private JRadioButton  jrbLoweredBevel , jrbRaisedBevel , jrbEtched , 
    jrbLine , jrbMatte , jrbEmpty; // Radio buttons for titled border options 
    private JRadioButton  jrbAboveBottom , jrbBottom , jrbBelowBottom , jrbAboveTop ,
    jrbTop , jrbBelowTop , jrbLeft , jrbCenter , jrbRight;

    // TitledBorder for the label                  -------------------???????
    private TitledBorder  jLabel1Border; /** Constructor **/
    public MainPanel(){
    // Create a JLabel instance and set colors
    jLabel1.setBackground(Color.red);
    jLabel1.setBorder(jLabel1Border);

    //11111111111111111111111111111111111
    // Place title position radio buttons
    JPanel jpPosition = new JPanel();
    jpPosition.setLayout(new GridLayout(3,2));
    jpPosition.add(jrbAboveBottom = new JRadioButton("ABOVE_BOTTOM"));
    jpPosition.add(jrbAboveTop = new JRadioButton("ABOVE_TOP"));
    jpPosition.add(jrbBottom = new JRadioButton("BOTTOM"));
    jpPosition.add(jrbTop = new JRadioButton("TOP"));
    jpPosition.add(jrbBelowBottom = new JRadioButton("BELOW_BOTTOM"));
    jpPosition.add(jrbBelowTop = new JRadioButton("BELOW_TOP"));
    jpPosition.setBorder(new TitledBorder("Position"));

    // Place title justification radio buttons
    JPanel jpJustification = new JPanel();
    jpJustification.setLayout(new GridLayout(3,1));
    jpJustification.add(jrbLeft = new JRadioButton("LEFT"));
    jpJustification.add(jrbCenter = new JRadioButton("CENTER"));
    jpJustification.add(jrbRight = new JRadioButton("RIGHT"));
    jpJustification.setBorder(new TitledBorder("Justification"));

    // Creat panel jpTitleOptions to how jpPosition and jpJustification
    JPanel jpTitleOptions = new JPanel();
    jpTitleOptions.setLayout(new BorderLayout());
    jpTitleOptions.add(jpPosition,BorderLayout.CENTER);
    jpTitleOptions.add(jpJustification,BorderLayout.EAST);

    // Create Panel jpTitle to hold a check box and title position radio
    // buttons, and title justification radio buttons
    JPanel jpTitle = new JPanel();
    jpTitle.setBorder(new TitledBorder("Border Title"));
    jpTitle.setLayout(new BorderLayout());
    jpTitle.add(jchkTitled = new JCheckBox("Titled"),BorderLayout.NORTH);
    jpTitle.add(jpTitleOptions , BorderLayout.CENTER);

    // Group radio buttons for title position
    ButtonGroup btgTitlePosition = new ButtonGroup();
    btgTitlePosition.add(jrbAboveBottom);
    btgTitlePosition.add(jrbAboveTop);
    btgTitlePosition.add(jrbBottom);
    btgTitlePosition.add(jrbTop);
    btgTitlePosition.add(jrbBelowBottom);
    btgTitlePosition.add(jrbBelowTop);

    // Group radio buttons for title justification
    ButtonGroup btgTitleJustification = new ButtonGroup();
    btgTitleJustification.add(jrbLeft);
    btgTitleJustification.add(jrbCenter);
    btgTitleJustification.add(jrbRight);
    //22222222222222222222222222222
    // Create Panel jpBorderStyle to hold border style radio buttons
    JPanel jpBorderStyle = new JPanel();
    jpBorderStyle.setBorder(new TitledBorder("Border Style"));
    jpBorderStyle.setLayout(new GridLayout(6,1));
    jpBorderStyle.add(jrbLoweredBevel = new JRadioButton("Lowered Bevel"));
    jpBorderStyle.add(jrbRaisedBevel = new JRadioButton("Raised Bevel"));
    jpBorderStyle.add(jrbEtched = new JRadioButton("Etched"));
    jpBorderStyle.add(jrbLine = new JRadioButton("Line"));
    jpBorderStyle.add(jrbMatte = new JRadioButton("Matte"));
    jpBorderStyle.add(jrbEmpty = new JRadioButton("Empty"));

    //Group radio buttons for border styles
    ButtonGroup btgBorderStyle = new ButtonGroup();
    btgBorderStyle.add(jrbLoweredBevel);
    btgBorderStyle.add(jrbRaisedBevel);
    btgBorderStyle.add(jrbEtched);
    btgBorderStyle.add(jrbLine);
    btgBorderStyle.add(jrbMatte);
    btgBorderStyle.add(jrbEmpty);

    // Create Panel jpAllChoices to place jpTitle and jpBorderStyle
    JPanel jpAllChoies = new JPanel();
    jpAllChoies.setLayout(new BorderLayout());
    jpAllChoies.add(jpTitle , "Center");
    jpAllChoies.add(jpBorderStyle, "East");

    // Place panels in the frame
    setLayout(new BorderLayout());
    add(jLabel1,"Center");
    add(jpAllChoies,"South");


    // Register listeners 
    ActionListener listener = new MyEventListener();
    jchkTitled.addActionListener(listener);

    jrbAboveBottom.addActionListener(listener);
    jrbAboveTop.addActionListener(listener);
    jrbBottom.addActionListener(listener);
    jrbTop.addActionListener(listener);
    jrbBelowBottom.addActionListener(listener);
    jrbBelowTop.addActionListener(listener);

    jrbLeft.addActionListener(listener);
    jrbCenter.addActionListener(listener);
    jrbRight.addActionListener(listener);

    jrbLoweredBevel.addActionListener(listener);
    jrbRaisedBevel.addActionListener(listener);
    jrbEtched.addActionListener(listener);
    jrbLine.addActionListener(listener);
    jrbMatte.addActionListener(listener);
    jrbEmpty.addActionListener(listener);

    }

    private class MyEventListener implements ActionListener {

    /* Handle ActionEvents on check box and radio buttons */
    public void actionPerformed(ActionEvent e){
    Border border = new EmptyBorder(2,2,2,2);

    if(jrbLoweredBevel.isSelected()){
    border = new BevelBorder(BevelBorder.LOWERED);
    jLabel1.setText("Lowered Bevel Style");
    }
    else if(jrbRaisedBevel.isSelected()){
    border = new BevelBorder(BevelBorder.RAISED);
    jLabel1.setText("Raised Bevel Style");
    }
    else if(jrbEtched.isSelected()){
    border = new EtchedBorder();
    jLabel1.setText("Etched Style");
    }
    else if(jrbLine.isSelected()){
    border = new LineBorder(Color.black,5);
    jLabel1.setText("Line Style");
    }
    else if(jrbMatte.isSelected()){
    }
    else if(jrbEmpty.isSelected()){
    border = new EmptyBorder(2,2,2,2);
    jLabel1.setText("Empty Style");
    }

    if(jchkTitled.isSelected()){
    //Get the title position and justification
    int titlePosition = TitledBorder.DEFAULT_POSITION;
    int titleJustification = TitledBorder.DEFAULT_JUSTIFICATION;

    if(jrbAboveBottom.isSelected())
    titlePosition = TitledBorder.ABOVE_BOTTOM;
    else if(jrbBottom.isSelected())
    titlePosition = TitledBorder.BOTTOM;
    else if(jrbBelowBottom.isSelected())
    titlePosition = TitledBorder.BELOW_BOTTOM;
    else if(jrbAboveTop.isSelected())
    titlePosition = TitledBorder.ABOVE_TOP;
    else if(jrbTop.isSelected())
    titlePosition = TitledBorder.TOP;
    else if(jrbBelowTop.isSelected())
    titlePosition = TitledBorder.BELOW_TOP;

    if(jrbLeft.isSelected())
    titleJustification = TitledBorder.LEFT;
    else if(jrbCenter.isSelected())
    titleJustification = TitledBorder.CENTER;
    else if(jrbRight.isSelected())
    titleJustification = TitledBorder.RIGHT;

    jLabel1Border = new TitledBorder("A Title");
    jLabel1Border.setBorder(border);
    jLabel1Border.setTitlePosition(titlePosition);
    jLabel1Border.setTitleJustification(titleJustification);
    jLabel1.setBorder(jLabel1Border);
    }
    else{
    jLabel1.setBorder(border);
    } }
    }
    }
      

  3.   

    被遮盖的内容是什么?
    那个没看清.
    更改的JLabel1你setText("");
    看原来的内容是否在。
    另外下次上传代码的时候带上Java格式,
    如下面的java代码
    最后,建议一下楼主多加注释
      

  4.   


    class test{
    public static void main(String[] args)throws Exception{
    MyFrame f = new MyFrame();
    f.setSize(500,300);
    f.setVisible(true);
    }//main
    }
             
    class MyFrame extends JFrame {
    public MyFrame()throws Exception{
       MainPanel mainPanel = new MainPanel();
       setLayout(new BorderLayout());
       add(mainPanel,"Center");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    }
    class MainPanel extends JPanel{
    // Declare a label for displaying message
    private JLabel jLabel1 = new JLabel("Display the border type",JLabel.CENTER);

    //A check box for seleting a border with or without a title
    private JCheckBox jchkTitled;

    // Radio buttons for border styles
    private JRadioButton  jrbLoweredBevel , jrbRaisedBevel , jrbEtched , 
    jrbLine , jrbMatte , jrbEmpty; // Radio buttons for titled border options 
    private JRadioButton  jrbAboveBottom , jrbBottom , jrbBelowBottom , jrbAboveTop ,
    jrbTop , jrbBelowTop , jrbLeft , jrbCenter , jrbRight;

    // TitledBorder for the label                  
    private TitledBorder  jLabel1Border; /** Constructor **/
    public MainPanel(){
    // Create a JLabel instance and set colors
    jLabel1.setBackground(Color.red);
    jLabel1.setBorder(jLabel1Border);

    //11111111111111111111111111111111111
    // Place title position radio buttons
    JPanel jpPosition = new JPanel();
    jpPosition.setLayout(new GridLayout(3,2));
    jpPosition.add(jrbAboveBottom = new JRadioButton("ABOVE_BOTTOM"));
    jpPosition.add(jrbAboveTop = new JRadioButton("ABOVE_TOP"));
    jpPosition.add(jrbBottom = new JRadioButton("BOTTOM"));
    jpPosition.add(jrbTop = new JRadioButton("TOP"));
    jpPosition.add(jrbBelowBottom = new JRadioButton("BELOW_BOTTOM"));
    jpPosition.add(jrbBelowTop = new JRadioButton("BELOW_TOP"));
    jpPosition.setBorder(new TitledBorder("Position"));

    // Place title justification radio buttons
    JPanel jpJustification = new JPanel();
    jpJustification.setLayout(new GridLayout(3,1));
    jpJustification.add(jrbLeft = new JRadioButton("LEFT"));
    jpJustification.add(jrbCenter = new JRadioButton("CENTER"));
    jpJustification.add(jrbRight = new JRadioButton("RIGHT"));
    jpJustification.setBorder(new TitledBorder("Justification"));

    // Creat panel jpTitleOptions to how jpPosition and jpJustification
    JPanel jpTitleOptions = new JPanel();
    jpTitleOptions.setLayout(new BorderLayout());
    jpTitleOptions.add(jpPosition,BorderLayout.CENTER);
    jpTitleOptions.add(jpJustification,BorderLayout.EAST);

    // Create Panel jpTitle to hold a check box and title position radio
    // buttons, and title justification radio buttons
    JPanel jpTitle = new JPanel();
    jpTitle.setBorder(new TitledBorder("Border Title"));
    jpTitle.setLayout(new BorderLayout());
    jpTitle.add(jchkTitled = new JCheckBox("Titled"),BorderLayout.NORTH);
    jpTitle.add(jpTitleOptions , BorderLayout.CENTER);

    // Group radio buttons for title position
    ButtonGroup btgTitlePosition = new ButtonGroup();
    btgTitlePosition.add(jrbAboveBottom);
    btgTitlePosition.add(jrbAboveTop);
    btgTitlePosition.add(jrbBottom);
    btgTitlePosition.add(jrbTop);
    btgTitlePosition.add(jrbBelowBottom);
    btgTitlePosition.add(jrbBelowTop);

    // Group radio buttons for title justification
    ButtonGroup btgTitleJustification = new ButtonGroup();
    btgTitleJustification.add(jrbLeft);
    btgTitleJustification.add(jrbCenter);
    btgTitleJustification.add(jrbRight);
    //22222222222222222222222222222
    // Create Panel jpBorderStyle to hold border style radio buttons
    JPanel jpBorderStyle = new JPanel();
    jpBorderStyle.setBorder(new TitledBorder("Border Style"));
    jpBorderStyle.setLayout(new GridLayout(6,1));
    jpBorderStyle.add(jrbLoweredBevel = new JRadioButton("Lowered Bevel"));
    jpBorderStyle.add(jrbRaisedBevel = new JRadioButton("Raised Bevel"));
    jpBorderStyle.add(jrbEtched = new JRadioButton("Etched"));
    jpBorderStyle.add(jrbLine = new JRadioButton("Line"));
    jpBorderStyle.add(jrbMatte = new JRadioButton("Matte"));
    jpBorderStyle.add(jrbEmpty = new JRadioButton("Empty"));

    //Group radio buttons for border styles
    ButtonGroup btgBorderStyle = new ButtonGroup();
    btgBorderStyle.add(jrbLoweredBevel);
    btgBorderStyle.add(jrbRaisedBevel);
    btgBorderStyle.add(jrbEtched);
    btgBorderStyle.add(jrbLine);
    btgBorderStyle.add(jrbMatte);
    btgBorderStyle.add(jrbEmpty);

    // Create Panel jpAllChoices to place jpTitle and jpBorderStyle
    JPanel jpAllChoies = new JPanel();
    jpAllChoies.setLayout(new BorderLayout());
    jpAllChoies.add(jpTitle , "Center");
    jpAllChoies.add(jpBorderStyle, "East");

    // Place panels in the frame
    setLayout(new BorderLayout());
    add(jLabel1,"Center");
    add(jpAllChoies,"South");


    // Register listeners 
    ActionListener listener = new MyEventListener();
    jchkTitled.addActionListener(listener);

    jrbAboveBottom.addActionListener(listener);
    jrbAboveTop.addActionListener(listener);
    jrbBottom.addActionListener(listener);
    jrbTop.addActionListener(listener);
    jrbBelowBottom.addActionListener(listener);
    jrbBelowTop.addActionListener(listener);

    jrbLeft.addActionListener(listener);
    jrbCenter.addActionListener(listener);
    jrbRight.addActionListener(listener);

    jrbLoweredBevel.addActionListener(listener);
    jrbRaisedBevel.addActionListener(listener);
    jrbEtched.addActionListener(listener);
    jrbLine.addActionListener(listener);
    jrbMatte.addActionListener(listener);
    jrbEmpty.addActionListener(listener);

    }

    private class MyEventListener implements ActionListener {

    /* Handle ActionEvents on check box and radio buttons */
    public void actionPerformed(ActionEvent e){
    Border border = new EmptyBorder(2,2,2,2);

    if(jrbLoweredBevel.isSelected()){
    border = new BevelBorder(BevelBorder.LOWERED);
    jLabel1.setText("Lowered Bevel Style");
    }
    else if(jrbRaisedBevel.isSelected()){
    border = new BevelBorder(BevelBorder.RAISED);
    jLabel1.setText("Raised Bevel Style");
    }
    else if(jrbEtched.isSelected()){
    border = new EtchedBorder();
    jLabel1.setText("Etched Style");
    }
    else if(jrbLine.isSelected()){
    border = new LineBorder(Color.black,5);
    jLabel1.setText("Line Style");
    }
    else if(jrbMatte.isSelected()){
    }
    else if(jrbEmpty.isSelected()){
    border = new EmptyBorder(2,2,2,2);
    jLabel1.setText("Empty Style");
    }

    if(jchkTitled.isSelected()){
    //Get the title position and justification
    int titlePosition = TitledBorder.DEFAULT_POSITION;
    int titleJustification = TitledBorder.DEFAULT_JUSTIFICATION;

    if(jrbAboveBottom.isSelected())
    titlePosition = TitledBorder.ABOVE_BOTTOM;
    else if(jrbBottom.isSelected())
    titlePosition = TitledBorder.BOTTOM;
    else if(jrbBelowBottom.isSelected())
    titlePosition = TitledBorder.BELOW_BOTTOM;
    else if(jrbAboveTop.isSelected())
    titlePosition = TitledBorder.ABOVE_TOP;
    else if(jrbTop.isSelected())
    titlePosition = TitledBorder.TOP;
    else if(jrbBelowTop.isSelected())
    titlePosition = TitledBorder.BELOW_TOP;

    if(jrbLeft.isSelected())
    titleJustification = TitledBorder.LEFT;
    else if(jrbCenter.isSelected())
    titleJustification = TitledBorder.CENTER;
    else if(jrbRight.isSelected())
    titleJustification = TitledBorder.RIGHT;

    jLabel1Border = new TitledBorder("A Title");
    jLabel1Border.setBorder(border);
    jLabel1Border.setTitlePosition(titlePosition);
    jLabel1Border.setTitleJustification(titleJustification);
    jLabel1.setBorder(jLabel1Border);
    }
    else{
    jLabel1.setBorder(border);
    } }
    }}
      

  5.   


    setText("");改了后点Button就成这样了
    我点的Lowered Bevel 后遮盖的内容就是 Lowered Bevel 
      

  6.   

    好吧,我还想还是没理解。
    这样吧。你原来预计的结果是应该出现什么?
    实际输出的结果是什么。
    发行来,不要发图片,发显示的内容。
    除了这个JAreaText覆盖,其他的没问题吧?