0
public class Pencere{
    JFrame pencere = new JFrame();
    JButton buton1, buton2;

    public Pencere() {
        pencere.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pencere.setTitle("Ana Pencere");
        pencere.setBackground(Color.getHSBColor(15f, 120f, 220f));
        pencere.setLocation(new Point(500, 300));
        pencere.setSize(613, 253);
        pencere.addMouseListener(new Dinle());
        pencere.setLayout(new FlowLayout());
        pencereDegis();
    }
    public void goster(){
        pencere.setVisible(true);
    }

    public void pencereDegis(){
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException | InstantiationException
                | IllegalAccessException | UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        buton2 = new JButton("Butonnn"); //This button not visible
        pencere.add(buton2);
        pencere.pack();
    }
}

当我运行我的应用程序(我显示 JFrame 窗口)时,所有代码都正常工作,但是当我没有看到“Butonnn”按钮后调用 pencereDegis() 方法时。

对不起我的英语不好谢谢大家..

4

2 回答 2

1

这段代码对我来说很适合goster()在方法中调用main。该按钮buton2是可见的(我只是取消了你的注释addMouseListener(),因为我没有Dinle上课)。我不确定,但这可能是因为你的LookAndFeel陈述。我有 OSX,第一次无法显示您的框架(因为您使用的是WindowsLookAndFeel),但是通过此更正它运行良好。

public class Pencere{
    JFrame pencere = new JFrame();
    JButton buton1, buton2;

    public Pencere() {
        pencere.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pencere.setTitle("Ana Pencere");
        pencere.setBackground(Color.getHSBColor(15f, 120f, 220f));
        pencere.setLocation(new Point(500, 300));
        pencere.setSize(613, 253);
        //pencere.addMouseListener(new Dinle());
        pencere.setLayout(new FlowLayout());
        pencereDegis();
    }
    public void goster(){
        pencere.setVisible(true);
    }

    public void pencereDegis(){
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException
                | IllegalAccessException | UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        buton2 = new JButton("Butonnn"); //This button not visible
        pencere.add(buton2);
        pencere.pack();
    }

    public static void main(String[] args)
    {
        Pencere p = new Pencere();
        p.goster();
    }
}
于 2013-03-10T12:14:22.710 回答
0
public static void main(String[] arg)
{
    pencereDegis();
    goster();
}

将其包含在您的代码中,它可以工作。

于 2013-03-10T12:33:42.013 回答