//去掉框架的布局管理器
JfsetLayout(null);
//手动设置标签面板的位置与宽高
JTabbedPane_TitlesetBounds(0,50,600,150)。
如在有布局的情况下,在JTabbedPane上方放一个高度为50的Panel或菜单栏。
JTabbedPane的使用:
类层次结构图:
javalangObject
--javaawtComponent
--javaawtContainer
--javaxswingJComponent
--javaxswingJTabbedPane
JTabbedPane构造函数:
JTabbedPane():建立一个空的JTabbedPane对象。
JTabbedPane(int tabPlacement):建立一个空的JTabbedPane对象,并指定摆放位置,如TOP,BOTTOM,LEFT,RIGHT
JTabbedPane的事件处理模式:
JTabbedPane以处理ChangeEvent事件为主。每当在JTabbedpane选换标签时,都会产生ChangeEvent事件。因此要处理选换标签所
对应的 *** 作,必须实现ChangeListener这个interface另外JTabbedPane上的每个标签都有索引值(index),一般若没有加以设置
,索引置从左到右依次是0,1,2,依此类推,因此下例中,Picture的索引值为0,Label 2的索引值为1。
一个标签都有所对应的 *** 作。
相对定位是利用布局管理器GridBagLayout() 来实现的GridBagLayout 布局是根据GridBagConstraints() 来设定的GridBagConstraints主要有8个重要参数需要掌握非别是
gridx,gridy —— 设置组件的位置,
gridx设置为GridBagConstraintsRELATIVE代表此组件位于之前所加入组件的右边。
gridy设置为GridBagConstraintsRELATIVE代表此组件位于以前所加入组件的下面。
建议定义出gridx,gridy的位置以便以后维护程序。gridx=0,gridy=0时放在0行0列。
gridwidth,gridheight —— 用来设置组件所占的单位长度与高度,默认值皆为1。
你可以使用GridBagConstraintsREMAINDER常量,代表此组件为此行或此列的最后一个组件,而且会占据所有剩余的空间。
weightx,weighty —— 用来设置窗口变大时,各组件跟着变大的比例。
当数字越大,表示组件能得到更多的空间,默认值皆为0。
anchor —— 当组件空间大于组件本身时,要将组件置于何处。
有CENTER(默认值)、NORTH、NORTHEAST、EAST、SOUTHEAST、WEST、NORTHWEST选择。
insets —— 设置组件之间彼此的间距。
它有四个参数,分别是上,左,下,右,默认为(0,0,0,0)。
ipadx,ipady —— 设置组件间距,默认值为0。
GridBagLayout里的各种设置都必须通过GridBagConstraints,因此当我们将GridBagConstraints的参数都设置
好了之后,必须new一个GridBagConstraints的对象出来,以便GridBagLayout使用。
构造函数:
GirdBagLayout()建立一个新的GridBagLayout管理器。
GridBagConstraints()建立一个新的GridBagConstraints对象。
GridBagConstraints(int gridx,int gridy,
int gridwidth,int gridheight,
double weightx,double weighty,
int anchor,int fill, Insets insets,
int ipadx,int ipady)建立一个新的GridBagConstraints对象,并指定其参数的值
下面是我的例程:
实现
import javaxswing;
import javautil;
import javaawt;
public class Example extends JFrame{
private void makeComponent(GridBagLayout gbLaout,GridBagConstraints constraints,Component component)
{
gbLaoutsetConstraints(component, constraints);
add(component);
}
public static void main(String args[]) {
Example exp=new Example();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
expsetLayout(gridbag);
JButton jb1=new JButton("JButton1");
cgridx=0;
cgridy=0;
cgridwidth=1;
cgridheight=1;
cweightx=1;
cweighty=0;
canchor=GridBagConstraintsCENTER;
cfill=GridBagConstraintsHORIZONTAL;
cinsets=new Insets(0,0,0,0);
cipadx=0;
cipady=0;
expmakeComponent(gridbag,c,jb1);
JButton jb2=new JButton("JButton2");
cgridx=1;
expmakeComponent(gridbag,c,jb2);
JButton jb3=new JButton("JButton2");
cgridx=2;
expmakeComponent(gridbag,c,jb3);
JButton jb4=new JButton("JButton2");
cgridx=3;
expmakeComponent(gridbag,c,jb4);
JButton jb5=new JButton("JButton5");
cgridx=0;
cgridy=1;
cgridheight=1;
cgridwidth=4;
expmakeComponent(gridbag,c,jb5);
JButton jb6=new JButton("JButton6");
cgridx=0;
cgridy=2;
cgridwidth=3;
expmakeComponent(gridbag,c,jb6);
JButton jb7=new JButton("JButton7");
cgridx=3;
cgridy=2;
cgridwidth=1;
expmakeComponent(gridbag,c,jb7);
JButton jb8=new JButton("JButton8");
cgridx=0;
cgridy=3;
cgridwidth=1;
cgridheight=2;
cfill=GridBagConstraintsBOTH;
expmakeComponent(gridbag,c,jb8);
JButton jb9=new JButton("JButton9");
cgridx=1;
cgridy=3;
cgridwidth=3;
cgridheight=1;
cfill=GridBagConstraintsHORIZONTAL;
expmakeComponent(gridbag,c,jb9);
JButton jb10=new JButton("JButton10");
cgridx=1;
cgridy=4;
cgridwidth=3;
cgridheight=1;
cfill=GridBagConstraintsHORIZONTAL;
expmakeComponent(gridbag,c,jb10);
expsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
expsetSize(500,500);
expsetVisible(true);
}
}
我已发送邮件到你的邮箱,我的邮箱是fengxk2004@163com
Java源程序附后。
本程序的特点是:
(1) 文本框只能输入纯数字;
(2) 界面较美观;
(3) 代码可读性较好,有适当的注释;
(4) 窗体一出现就在桌面居中。
import javaawt;
import javaawtevent;
import javautil;
import javaxswing;
public class GuessNumber {
private static final long serialVersionUID = 1L;
JFrame frame;
JTextField txtNum; //文本框
JButton btnStart; //按钮
JLabel lblPrompt;
JLabel lblMessage;
static int source = 0;
static Random rand = new Random();
public GuessNumber(){
frame = new JFrame("Guess Number");
JPanel pnl1, pnl2, pnl3, pnl4;
pnl1 = new JPanel();
pnl1setLayout(new FlowLayout(FlowLayoutLEFT));
pnl2 = new JPanel();
pnl2setLayout(new FlowLayout(FlowLayoutLEFT));
pnl3 = new JPanel();
pnl3setLayout(new FlowLayout(FlowLayoutLEFT));
pnl4 = new JPanel();
pnl4setLayout(new FlowLayout(FlowLayoutLEFT));
txtNum = new JTextField(10);
btnStart = new JButton("开始");
lblPrompt = new JLabel("<html><body>I have a number between 1 and 1000 can you guess my number<br/>Please enter your first guess</body></html>");
lblMessage = new JLabel();
pnl1add(lblPrompt);
pnl2add(txtNum);
pnl3add(lblMessage);
pnl4add(btnStart);
framesetLayout(new GridLayout(4, 1));
frameadd(pnl1);
frameadd(pnl2);
frameadd(pnl3);
frameadd(pnl4);
txtNumaddActionListener(thisnew TextAction());
txtNumaddKeyListener(thisnew KeyAction());
btnStartaddActionListener(thisnew ButtonAction());
framesetSize(400, 200);
framesetVisible(true);
framesetLocationRelativeTo(null);
framesetResizable(false);
}
public static void main(String[] args) {
new GuessNumber();
while((source=randnextInt(1000))==0);
}
//按钮单击后的事件处理
class ButtonAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)egetSource();
if(btn == btnStart){
while((source=randnextInt(1000))==0);
txtNumsetEditable(true);
}
}
}
//文本框按回车后的事件处理
class TextAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JTextField txt = (JTextField)egetSource();
if(txt != txtNum){
return;
}
int num = IntegerparseInt(txtNumgetText());
if(num == source){
lblMessagesetText("Correct!");
txtNumsetEditable(false);
txtNumsetBackground(framegetBackground());
}
else if(num > source){
lblMessagesetText("Too High");
txtNumsetBackground(Colorred);
}
else{
lblMessagesetText("Too Low");
txtNumsetBackground(Colorblue);
}
}
}
//限制文本框只能输入数字
class KeyAction implements KeyListener{
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
int k = egetKeyChar();
String text = ((JTextField)egetSource())getText();
if(!((k>47 && k <58) || (k==8 || k==KeyEventVK_PERIOD))){ //限制只能输入数字
esetKeyChar((char)KeyEventVK_CLEAR);
}
if(textlength() > 4){ //限制数值的长度
esetKeyChar((char)KeyEventVK_CLEAR);
}
}
}
}
以上就是关于swing关于JTabbedPane的位置全部的内容,包括:swing关于JTabbedPane的位置、java gui中怎么用jpanel实现组件的绝对定位、java的一个窗体建立等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)