0

为什么第一个代码没有在 JPanel 周围放置边框,而第二个代码呢?我看不出边框代码有任何差异。在第一个中,我正在研究蛇。第二个是我写在这里发布的一个例子,但结果证明它有效。

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class ts {


public static void main(String[] args) {

        MyJFrame frame1 = new MyJFrame();

    }
    static class MyJFrame extends JFrame{

        public MyJFrame (){

            setTitle("Snake Build: Dev 0.2");
            setBounds(200,100,600,500);
            setResizable(false);
            setLayout(null);

            Container cp = getContentPane();

            JPanel settings = new JPanel();
            settings.setBounds(200, 100, 200, 200);
            settings.setBorder(BorderFactory.createRaisedBevelBorder());
            //exsists but cant be seen border working?
            settings.addMouseListener(new MouseListener(){
                @Override
                public void mouseClicked(MouseEvent arg0) {
                }

                @Override
                public void mouseEntered(MouseEvent arg0) {
                    System.out.println("entered");
                }

                @Override
                public void mouseExited(MouseEvent arg0) {
                    System.out.println("exited");
                }

                @Override
                public void mousePressed(MouseEvent arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void mouseReleased(MouseEvent arg0) {
                    // TODO Auto-generated method stub

                }
            });

            cp.add(settings);
            settings.setVisible(true);

            JButton test = new JButton("test");
            test.setBounds(10, 20, 30, 30);
            settings.add(test);
            test.setVisible(true);

            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
        }

        public void paint(Graphics g)
        {

        }
    }
}

第二件确实给出了边界

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class ts {
    public static void main(String[] args) {

        MyJF frame1 = new MyJF();

    }
    static class MyJF extends JFrame{
        public MyJF(){
            setBounds(50,50,500,200);
            setLayout(null);
            setResizable(false);

            Container cp = getContentPane();

            JPanel settings = new JPanel();
            settings.setBounds(20,20,100,100);
            settings.setBorder(BorderFactory.createRaisedBevelBorder());

            JButton JB = new JButton("hey");
            JB.setBounds(10, 10, 50, 50);
            settings.add(JB);

            add(settings);

            settings.setVisible(true);

            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }

}
4

1 回答 1

3

Without wanting to state the obvious but you are overriding JFrame.paint(Graphics) with an empty code-block, hence you see problems. If you remove that part it will work as expected.

Consider the following recommendations:

  • Do not override JFrame.paint(Graphics) but instead override JComponent.paintComponent(Graphics) on a component contained in your JFrame for custom painting. In both case, you should usually also call the super method
  • Use appropriate LayoutManager's and forget about setBounds/setLocation/setSize (and don't try to replace them with set-Min/Max/Pref-Size)
  • Follow Java naming conventions (classes start with an Upper case letter)
  • Do not extend JFrame if it is not needed. You should extend JFrame only if you add frame-behaviour
于 2012-10-31T13:40:32.710 回答