JPanel panel = new buttonPanel()
panel setBorder(BorderFactory createEmptyBorder( ) //设置面板的margin或者说是padding为
panel setLayout(new GridLayout( )) //把面板设为网格布局管理器 行 列 组件间水平距离 垂直距离
JButton button = new JButton( XXX )
lishixinzhi/Article/program/Java/hx/201311/26789
我有点不太明白你的意思,Java写的控件,放在Layout里,我现在理解是把Button放到一个容器里。如果是这样的话,那就简单了,有个add()方法的。比如:
public class Test extends JFrame
{
public Test()
{
Contianer contentPane = this.getContentPane()
contentPane.setLayout(new BorderLayout()) //设置为BorderLayout布局
contentPane.add(new Button("按键") , BorderLayout.NORTH) //放在北边
contentPane.add(new Button("按键") , BorderLayout.SOUTH) //放在南边
setVisible(true)
setSize(400,300)
}
public static void main(String args[])
{
new Test()
}
}
(代码没有调试过,自己调试吧)
基本上就可以 把Button入进去了,不知道你是这个意思没。
你的JPanel可以用BorderLayout啊上面的文本域,可以被JScrollPane包裹啊,
然后这个pane就加到BorderLayout.CENTER
下面的按钮就加到BorderLayout.SOUTH
这样就刚刚好了,用布局要看情况来选择合适的布局。
还是跟你写好了吧,说都白说了
import java.awt.BorderLayout
import java.awt.FlowLayout
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JPanel
import javax.swing.JScrollPane
import javax.swing.JTextArea
public class JButtonTest extends JFrame
{
private static final long serialVersionUID = 1L
public JButtonTest ()
{
setTitle ("tester")
setSize (300, 300)
setLayout (new BorderLayout ())
setLocationRelativeTo (null)
setResizable (false)
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE)
}
private void addComponents ()
{
JTextArea area = new JTextArea ()
JScrollPane scrollPane = new JScrollPane (area)
JButton button = new JButton ("图书推荐")
add (scrollPane, BorderLayout.CENTER)
JPanel panel = new JPanel (new FlowLayout (FlowLayout.CENTER))
panel.add (button)
add (panel, BorderLayout.SOUTH)
}
public static void main ( String[] args )
{
JButtonTest tester = new JButtonTest ()
tester.addComponents ()
tester.setVisible (true)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)