gpt4 book ai didi

java - 如何在 Blue J 中设置 JButton 的坐标?

转载 作者:行者123 更新时间:2023-11-30 08:14:55 25 4
gpt4 key购买 nike

我试图在 Blue J 中设置 Jbutton 的坐标,我试过 button.setbounds 但它说不存在。这是我到目前为止所拥有的:

  public void makeButtons()
{
// create buttons
buttonL1 = new JButton( "Button L1" );
add( buttonL1);
buttonL1.setbounds( 220, 180, (int) rHeight, (int) rWidth );
buttonL2 = new JButton( "Button L2" );
add( buttonL2 );
buttonR3 = new JButton( "Button R3" );
c.add( buttonR3 );
buttonR4 = new JButton( "Button R4" );
c.add( buttonR4 );

ButtonHandler handler = new ButtonHandler();
buttonL1.addActionListener( handler );
buttonL2.addActionListener( handler );
buttonR3.addActionListener( handler );
buttonR4.addActionListener( handler );
c.repaint();
}



/**
* Button Event Handling
*/
private class ButtonHandler implements ActionListener {
public void actionPerformed( ActionEvent e )
{
JOptionPane.showMessageDialog( null,
"You pressed: " + e.getActionCommand() );
}
}

最佳答案

你是对的,setbounds 不存在,但 setBounds(...) 实际上存在——大小写很重要,看这个的技巧也很重要在 API 中。但话说回来,不要使用 null 布局或 setBounds(...),因为这会导致创建僵化的 GUI,可能在使用该操作系统和屏幕分辨率的一台计算机上看起来不错,但在其他地方看起来很糟糕,将创建在 JScrollPanes 中不起作用的 JPanel,并导致不灵活的 GUI,这是皇家女巫调试或增强。而是使用布局管理器来帮助您放置组件。

另请注意,您的问题与 BlueJ 无关,BlueJ 只不过是您用来创建程序的 IDE 工具,并且与 Java 和 Swing 有关。

关于java - 如何在 Blue J 中设置 JButton 的坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29221958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com