简体   繁体   中英

not displaying in JPanel in GridLayout

I have a JFrame with GridLayout as its layoutmanager. When I press 'New...' (JMenuItem) new ColorPanel object with background:blue is added, but the text "test" does not show up inside the JPanel.The ColorPanel objects are being added and showing up fine as blue, but the white text that is supposed to be inside is not showing up. I tried adding getX()+20,getY()+20 in drawString(... ,x,y); its behaving odd, and not showing up in the right place, or not showing up at all. How do I make the text appear inside the JPanel which have been added to frame via GridLayout.

public class MainFrame{
    protected JFrame frame;
    private JPanel containerPanel;
    private GridLayout gl = new GridLayout(3,1);

public MainFrame(){
    frame = new JFrame("Test");
    containerPanel = new JPanel();
    gl.setHgap(3);gl.setVgap(3);
    frame.setLayout(gl);
    frame.setSize(500, 500);
    frame.setJMenuBar(getMenuBar());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
    frame.setVisible(true);
}

public JMenuBar getMenuBar(){
    JMenuBar menu = new JMenuBar();
    JMenu file = new JMenu("File");
    JMenuItem newItem = new JMenuItem("New...");
    newItem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            frame.add(new ColorPanel());
            frame.getContentPane().revalidate();
        }
    });
    file.add(newItem);
    menu.add(file);
    return menu;
}
private class ColorPanel extends JPanel{
    ColorPanel(){
        setBackground(Color.BLUE);
        setPreferredSize(new Dimension(150,150));
        setVisible(true);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.WHITE);
        System.out.println("X:"+getX()+"Y:"+getY());
        g.drawString("Test", getX(), getY());
    }
}
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable(){
        @Override
        public void run() {
            new MainFrame();
        }
    });
}

}

I tried adding getX()+20,getY()+20 in drawString(... ,x,y); its behaving odd, and not showing up in the right place

The x/y values represent the bottom/left position of the text NOT the top/left.

So if you want to do custom painting you must determine the proper x/y values so that the text does "show up in the right place", whatever that means to you.

You will need to use the FontMetrics class if you want to do any fancy positioning of the text. You can get the FontMetrics from the Graphics object.

getX() and getY() returns the x/y position of the component within the context of it's parent.

All references within a component are relative to that component, meaning that the top left/top position of the component is actually 0x0 .

This likely means that the text is been painted off the visible area of the component.

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.WHITE);
    FontMetrics fm = g.getFontMetrics();
    int y = fm.getFontHeight() + fm.getAscent();
    System.out.println("X:"+getX()+"Y:"+getY());
    g.drawString("Test", 0, y);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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