简体   繁体   English

单击JmenuItem时更新边框标题

[英]Update border title when JmenuItem is clicked

My question is a little complicated please,Am trying to update the borderTitle of a Jpanel when a JMenuItem is clicked,i have 3 classes,A implements ActionListener, B is the JPanel class and C is the JFrame class,here is what i have tried already 请问我的问题有点复杂,单击JMenuItem时尝试更新Jpanel的borderTitle,我有3个类,A实现ActionListener,B是JPanel类,C是JFrame类,这是我尝试过的已经

public class PanelTitle implements ActionListener{
     String title;
    public PanelTitle(){
    }                     
    @Override
    public void actionPerformed(ActionEvent ae){
         SedimentPanel sp = new SedimentPanel();
         sp.titledBorder.setTitle("SEDIMENT");

         sp.repaint();
         sp.revalidate();


    }
}

i have this in my JFrame class 我在我的JFrame类中有这个

 velocityMenuItem.addActionListener(new PanelTitle());

here is my JPanel class 这是我的JPanel课程

public class SedimentPanel extends JPanel{
    public SedimentPanel(){
        super();
        initComponents();
        initPlaceHolders();
        setBorder(titledBorder); 

    }

    TitledBorder titledBorder = BorderFactory.createTitledBorder(null, "border title",TitledBorder.CENTER,TitledBorder.DEFAULT_POSITION);

}

please how do i really get the borderTitle to change when i click a JMenuItem? 请单击JMenuItem时如何真正更改borderTitle? Here is how i referenced it in the frame class,now i get a Nullpointer Exception 这是我在框架类中引用它的方式,现在我收到了Nullpointer异常

public class FrameClass extends JFrame{
private static SedimentPanel sp;
    public FrameClass(SedimentPanel sp){
        this.sp = sp;}
}
public static void main(String args[]){
 FrameClass fc = new FrameClass(sp);
}

You're making a basic mistake here: 您在这里犯了一个基本错误:

public class PanelTitle implements ActionListener{
     String title;
    public PanelTitle(){
    }                     
    @Override
    public void actionPerformed(ActionEvent ae){
         SedimentPanel sp = new SedimentPanel(); // ********
         sp.titledBorder.setTitle("SEDIMENT");

         sp.repaint();
         sp.revalidate();
    }
}

That new SedimentPanel is a completely new reference and calling a method on it will have no effect on the original displayed object. 新的SedimentPanel是一个全新的引用,对其调用方法将不会对原始显示的对象产生影响。 Don't do this, get the appropriate reference and call the method on it. 不要这样做,获取适当的引用并在其上调用方法。

public class PanelTitle implements ActionListener{
    String title;
    private SedimentPanel sp;

    public PanelTitle(SedimentPanel sp){  // pass in reference
        this.sp = sp;
    }

    @Override
    public void actionPerformed(ActionEvent ae){
         // SedimentPanel sp = new SedimentPanel(); // ******** NO
         // sp.titledBorder.setTitle("SEDIMENT");
         sp.setTitle("SEDIMENT"); // better to give the class this method
         sp.repaint();
         sp.revalidate();
    }
}

public class SedimentPanel {
    private TitledBorder titledBorder = ....;

    public void setTitle(String title) {
        titledBorder.setText(title);
    }
}   

Then when you create this listener, pass in the appropriate reference to the actual visualized JPanel. 然后,在创建此侦听器时,将适当的引用传递给实际的可视化 JPanel。

That you're making this mistake suggests that it wouldn't harm you to read or re-read a decent chapter in your text on what an object/reference is and what it represents because this is a foundational mistake that you're making. 您正在犯此错误表明,阅读或重新阅读文本中有关对象/引用是什么以及它代表什么的不错的章节不会对您造成危害,因为这是您所犯的根本性错误。

A working example: 一个工作示例:

import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class TitleExample {

    private static void createAndShowGui() {
        SedimentPanel sedimentPanel = new SedimentPanel();
        PanelTitle panelTitle = new PanelTitle(sedimentPanel); // pass in the reference
        JMenuItem menuItem = new JMenuItem("SEDIMENT");
        menuItem.addActionListener(panelTitle);
        JMenu jMenu = new JMenu("Menu");
        jMenu.add(menuItem);
        JMenuBar menuBar = new JMenuBar();
        menuBar.add(jMenu);

        JFrame frame = new JFrame("TitleExample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(sedimentPanel);
        frame.setJMenuBar(menuBar);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

class SedimentPanel extends JPanel {
    private TitledBorder titledBorder = BorderFactory.createTitledBorder(null, "border title", TitledBorder.CENTER,
            TitledBorder.DEFAULT_POSITION);

    public SedimentPanel() {
        super();
        setBorder(titledBorder);
        setPreferredSize(new Dimension(400, 300));
    }

    public void setTitle(String title) {
        titledBorder.setTitle(title);
        repaint();
    }

}

class PanelTitle implements ActionListener{
    String title;
    private SedimentPanel sp;

    public PanelTitle(SedimentPanel sp){  // pass in reference
        this.sp = sp;
    }

    @Override
    public void actionPerformed(ActionEvent ae){
         sp.setTitle("SEDIMENT"); // better to give the class this method
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM