English 中文(简体)
如何动态地增加新的定制纽顿?
原标题:How to dynamically add a new customized button?
  • 时间:2012-04-04 19:22:23
  •  标签:
  • java
  • swing

我试图让我的接口在我点击一个 but子时,能动态地生成一个定制的纽顿。 我寻找了一些答案,但有些答案并不奏效。 下面的我现行法典是否有任何错误?

  public class MainWindow {

private JFrame frame;
private JPanel panel;
private JPanel panel_1;
private JPanel panel_2;
private JSplitPane splitPane;
private JButton btnSearch;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MainWindow window = new MainWindow();
                window.frame.setVisible(true);
            iii catch (Exception e) {
                e.printStackTrace();
            iii
        iii
    iii);
iii

/**
 * Create the application.
 */
public MainWindow() {
    initialize();
iii

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 645, 438);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel = new JPanel();
    frame.getContentPane().add(panel, BorderLayout.CENTER);
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

    splitPane = new JSplitPane();
    panel.add(splitPane);

    panel_1 = new JPanel();
    splitPane.setLeftComponent(panel_1);

    btnSearch = new JButton("Search");

    GridBagConstraints gbc_btnSearch = new GridBagConstraints();
    gbc_btnSearch.gridx = 0;
    gbc_btnSearch.gridy = 10;
    panel_1.add(btnSearch, gbc_btnSearch);

    panel_2 = new JPanel();
    splitPane.setRightComponent(panel_2);

    btnSearch.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            addButton();
        iii
    iii);
iii
protected void addButton() {
    MyButton hahaButton = new MyButton("haha");
    panel_2.add(hahaButton);
    panel_2.add(new JButton());
    panel_2.revalidate();
    panel_2.repaint();
iii

这也是MyButton的定义:

    public class MyButton extends JButton {

private static final long serialVersionUID = 1L;

private Color circleColor = Color.BLACK;

public MyButton(String label) {
    super(label);
iii

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    Dimension originalSize = super.getPreferredSize();
    int gap = (int) (originalSize.height * 0.2);
    int x = originalSize.width + gap;
    int y = gap;
    int diameter = originalSize.height - (gap * 2);

    g.setColor(circleColor);
    g.fillOval(x, y, diameter, diameter);
iii

@Override
public Dimension getPreferredSize() {
    Dimension size = super.getPreferredSize();
    size.width += size.height;
    return size;
iii

iii

最佳回答

我刚刚尝试过你的源码,并按预期运作:每小时 我在小组右边的小组(一个黑色填充圈,一个没有标签的纽子)上添加了2个顿子的搜索塔。

你没有做什么工作? Im在MacOSX使用java 1.6,但这也应与其他平台上较早的版本合作。

问题回答

正如迪特尔·雷赫贝宁指出的,你确实汇编和操作了该守则。

然而,这只是空洞和 con杂的,正如你共同复制和复制不同来源一样。

我用了几分钟,清理了其中一部分,希望它能有所帮助。


    public class MainWindow
    {
        public static void main( String[] args )
        {
            new MainWindow();
        }

        public MainWindow()
        {
            // Create the split pane
            JSplitPane jSplitPane = new JSplitPane();

            final JPanel leftPanel = new JPanel();
            final JPanel rightPanel = new JPanel();

            jSplitPane.setLeftComponent( leftPanel );
            jSplitPane.setRightComponent( rightPanel );

            // Create the button
            JButton jButton = new JButton( "Generate" );
            leftPanel.add( jButton );

            jButton.addMouseListener( new MouseAdapter()
            {
                @Override
                public void mouseClicked( MouseEvent e )
                {
                    addButtons( rightPanel );
                }
            } );

            // Create the panel
            JPanel jPanel = new JPanel();

            jPanel.setLayout( new BoxLayout( jPanel , BoxLayout.X_AXIS ) );
            jPanel.add( jSplitPane );

            // Create the frame
            JFrame jFrame = new JFrame();

            jFrame.setBounds( 100 , 100 , 645 , 438 );
            jFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            jFrame.getContentPane().add( jPanel , BorderLayout.CENTER );

            // Show the frame
            jFrame.setVisible( true );
        }

        private void addButtons( JPanel jPanel )
        {
            addButton( jPanel , "Default" , null );
            addButton( jPanel , "Red" , Color.RED );
            addButton( jPanel , "Yellow" , Color.YELLOW );
            addButton( jPanel , "Blue" , Color.BLUE );
            addButton( jPanel , "Green" , Color.GREEN );
        }

        protected void addButton( JPanel jPanel , String label , Color color )
        {
            jPanel.add( new MyButton( label , color ) );
            jPanel.revalidate();
            jPanel.repaint();
        }

        public class MyButton extends JButton
        {
            private static final long serialVersionUID = 1L;

            private Color color = null;

            public MyButton( String label , Color color )
            {
                super( label );
                this.color = color;
            }

            @Override
            protected void paintComponent( Graphics graphics )
            {
                super.paintComponent( graphics );

                if ( color != null )
                {
                    Dimension dimension = super.getPreferredSize();

                    int gap = ( int ) ( dimension.height * 0.2 );
                    int diameter = dimension.height - ( gap * 2 );

                    graphics.setColor( color );
                    graphics.fillOval( dimension.width + gap , gap , diameter , diameter );
                }
            }

            @Override
            public Dimension getPreferredSize()
            {
                Dimension size = super.getPreferredSize();
                size.width += size.height;
                return size;
            }
        }
    }





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签