问题描述
publicOLHTool()throwsIOException{/*匹配当前系统外观*/try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch(Exceptione){thrownewRuntimeException(e);}olhWin=newFrame("OLHTool");BorderLayoutborderLayout1=newBorderLayout();Buttonstart=newButton("Start");Buttonsave=newButton("Save");JPanelbuttonlist=newJPanel(newFlowLayout());buttonlist.add(start);buttonlist.add(save);olhWin.add("South",buttonlist);start.setActionCommand("start");save.setActionCommand("save");start.addActionListener(this);save.addActionListener(this);//设置事件监听MenuBarmb=newMenuBar();Menum1=newMenu("File");MenuItemm11=newMenuItem("Open");MenuItemm12=newMenuItem("Exit");m11.setActionCommand("open");m12.setActionCommand("exit");m11.addActionListener(this);m12.addActionListener(this);m11.setShortcut(newMenuShortcut(KeyEvent.VK_O));//添加快捷键O--openm12.setShortcut(newMenuShortcut(KeyEvent.VK_E));//添加快捷键E--exitm1.add(m11);m1.addSeparator();m1.add(m12);mb.add(m1);olhWin.setMenuBar(mb);left=newTextArea();olhWin.add(left);right=newTextArea();olhWin.add(right);//添加文本区left.setFont(newFont("隶书",1,15));right.setFont(newFont("隶书",1,15));//定义文本区字体right.setEditable(false);left.setEditable(false);//文本区不可编辑JScrollPanescroldisplay=newJScrollPane(right);JScrollPanescrolme=newJScrollPane(left);//添加滚动条JSplitPanesplitpane=newJSplitPane(JSplitPane.HORIZONTAL_SPLIT,scrolme,scroldisplay);//文本区分隔splitpane.setDividerSize(10);splitpane.setOneTouchExpandable(true);splitpane.setContinuousLayout(true);olhWin.add("Center",splitpane);olhWin.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(1);}});olhWin.setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);olhWin.setLocation(0,100);olhWin.validate();olhWin.pack();olhWin.setVisible(true);}
解决方案
解决方案二:
css
解决方案三:
setLayout(null);//布局为空的时候,小学习项目的话。根据自己的喜好,想怎么来就这么来吧。
解决方案四:
setLayout(null);add控件a后,a.setBounds(intx,inty,intwidth,intheight);其中x,y为位置坐标,可以利用dth,height来随意定义控件的大小
解决方案五:
setLayout(null);//布局为空的时候,小学习项目的话。根据自己的喜好,想怎么来就这么来吧。
解决方案六:
引用3楼zxqwxyql的回复:
setLayout(null);add控件a后,a.setBounds(intx,inty,intwidth,intheight);其中x,y为位置坐标,可以利用dth,height来随意定义控件的大小
精确,书上就是这么讲的
解决方案七:
/*匹配当前系统外观*/try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch(Exceptione){thrownewRuntimeException(e);}olhWin=newFrame("OLHTool");BorderLayoutborderLayout1=newBorderLayout();//多余的Buttonstart=newButton("Start");Buttonsave=newButton("Save");start.setPreferredSize(newDimension(100,25));save.setPreferredSize(newDimension(100,25));JPanelbuttonlist=newJPanel();finalFlowLayoutflowLayout=newFlowLayout();flowLayout.setHgap(40);buttonlist.setLayout(flowLayout);buttonlist.add(start);buttonlist.add(save);olhWin.add("South",buttonlist);start.setActionCommand("start");save.setActionCommand("save");start.addActionListener(this);save.addActionListener(this);//设置事件监听MenuBarmb=newMenuBar();Menum1=newMenu("File");MenuItemm11=newMenuItem("Open");MenuItemm12=newMenuItem("Exit");m11.setActionCommand("open");m12.setActionCommand("exit");m11.addActionListener(this);m12.addActionListener(this);m11.setShortcut(newMenuShortcut(KeyEvent.VK_O));//添加快捷键O--openm12.setShortcut(newMenuShortcut(KeyEvent.VK_E));//添加快捷键E--exitm1.add(m11);m1.addSeparator();m1.add(m12);mb.add(m1);olhWin.setMenuBar(mb);left=newTextArea();//olhWin.add(left);多余的right=newTextArea();//olhWin.add(right);//添加文本区多余的left.setFont(newFont("隶书",1,15));right.setFont(newFont("隶书",1,15));//定义文本区字体right.setEditable(false);left.setEditable(false);//文本区不可编辑JScrollPanescroldisplay=newJScrollPane(right);scroldisplay.setBorder(newTitledBorder("right"));JScrollPanescrolme=newJScrollPane(left);//添加滚动条scrolme.setBorder(newTitledBorder("left"));JSplitPanesplitpane=newJSplitPane(JSplitPane.HORIZONTAL_SPLIT,scrolme,scroldisplay);//文本区分隔splitpane.setDividerSize(10);splitpane.setOneTouchExpandable(true);splitpane.setContinuousLayout(true);olhWin.add("Center",splitpane);olhWin.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(1);}});olhWin.setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);olhWin.setLocation(0,100);olhWin.validate();olhWin.pack();olhWin.setVisible(true);
如果要增加logo则要修改你目前的layout因此直接价格titleborder反而更直观,button可以修改setPreferredSize属性自己看红色代码
解决方案八:
/*匹配当前系统外观*/try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch(Exceptione){thrownewRuntimeException(e);}olhWin=newFrame("OLHTool");BorderLayoutborderLayout1=newBorderLayout();//多余的Buttonstart=newButton("Start");Buttonsave=newButton("Save");start.setPreferredSize(newDimension(100,25));save.setPreferredSize(newDimension(100,25));JPanelbuttonlist=newJPanel();finalFlowLayoutflowLayout=newFlowLayout();flowLayout.setHgap(40);buttonlist.setLayout(flowLayout);buttonlist.add(start);buttonlist.add(save);olhWin.add("South",buttonlist);start.setActionCommand("start");save.setActionCommand("save");start.addActionListener(this);save.addActionListener(this);//设置事件监听MenuBarmb=newMenuBar();Menum1=newMenu("File");MenuItemm11=newMenuItem("Open");MenuItemm12=newMenuItem("Exit");m11.setActionCommand("open");m12.setActionCommand("exit");m11.addActionListener(this);m12.addActionListener(this);m11.setShortcut(newMenuShortcut(KeyEvent.VK_O));//添加快捷键O--openm12.setShortcut(newMenuShortcut(KeyEvent.VK_E));//添加快捷键E--exitm1.add(m11);m1.addSeparator();m1.add(m12);mb.add(m1);olhWin.setMenuBar(mb);left=newTextArea();//olhWin.add(left);多余的right=newTextArea();//olhWin.add(right);//添加文本区多余的left.setFont(newFont("隶书",1,15));right.setFont(newFont("隶书",1,15));//定义文本区字体right.setEditable(false);left.setEditable(false);//文本区不可编辑JScrollPanescroldisplay=newJScrollPane(right);scroldisplay.setBorder(newTitledBorder("right"));JScrollPanescrolme=newJScrollPane(left);//添加滚动条scrolme.setBorder(newTitledBorder("left"));JSplitPanesplitpane=newJSplitPane(JSplitPane.HORIZONTAL_SPLIT,scrolme,scroldisplay);//文本区分隔splitpane.setDividerSize(10);splitpane.setOneTouchExpandable(true);splitpane.setContinuousLayout(true);olhWin.add("Center",splitpane);olhWin.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(1);}});olhWin.setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);olhWin.setLocation(0,100);olhWin.validate();olhWin.pack();olhWin.setVisible(true);如果要增加logo则要修改你目前的layout因此直接价格titleborder反而更直观,button可以修改setPreferredSize属性自己看红色代码格式错了