IDEA中怎么设置项目更改后的分析延迟时间,IDEA是java编程语言的集成开发环境,使用该软件编写代码时,可以设置项目更改后的分析延迟时间,这样就可确保项目范围分析的正确性,具体的操作步骤如下。......
2023-07-04 594 JAVA
工具栏是程序中常见的一种组件,比如Word和Excel工作区上方的工具栏。JavaSwing也有工具栏组件:JToolBar,具体怎么使用呢?接下来小编就整理分享给大家。
常用的构造方法如下:
构造方法1:
JToolBar()创建新的工具栏,默认的方向为 HORIZONTAL(水平)
构造方法2:
JToolBar(int orientation)创建具有指定 orientation 的新工具栏
构造方法3:
JToolBar(String name)创建一个具有指定 name 的新工具栏
构造方法4:
JToolBar(String name,int orientation)创建一个具有指定 name 和 orientation 的新工具栏
常用方法如下:
创建JFrame容器,设置容器的名称,容器的内容窗格:Demo27_JToolBar
内容窗格Demo27_JToolBar目前还是空的,接下来我们把它填满
Demo27_JToolBar是我们自定义的一个类,它继承JPanel,并且实现ActionListener接口
完成之后的效果如下:
点击工具栏上的按钮,会在下方显示对应的内容
Demo27_JToolBar类代码如下:
public class Demo27_JToolBar extends JPanel implements ActionListener{
protected JTextArea textArea;
protected String newline="\n";
static final private String OPEN="OPEN";
static final private String SAVE="SAVE";
static final private String NEW="NEW";
public static void main(String[] args){
JFrame.setDefaultLookAndFeelDecorated(true);
//定义窗体
JFrame frame=new JFrame("工具栏");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//定义面板
Demo27_JToolBar demo27_JToolBar=new Demo27_JToolBar();
demo27_JToolBar.setOpaque(true);
frame.setContentPane(demo27_JToolBar);
SwingUtils.setCenter(frame);//设置窗体大小600*800并居中
//显示窗体
frame.setVisible(true);
}
//事件监听器部分的代码省略,请查阅源文件
private void displayResult(String actionDescription){
textArea.append(actionDescription newline);
}
public Demo27_JToolBar() {
super(new BorderLayout());
//创建工具栏
JToolBar toolBar=new JToolBar();
addButtons(toolBar);
//创建一个文本域,用来输出一些信息
textArea=new JTextArea(15, 30);
textArea.setEditable(false);
textArea.setFont(new Font("宋体", 1, 20));
JScrollPane scrollPane=new JScrollPane(textArea);
//把组件添加到面板中
setPreferredSize(new Dimension(450, 110));
add(toolBar,BorderLayout.PAGE_START);
add(scrollPane,BorderLayout.CENTER);
}
private void addButtons(JToolBar toolBar) {
JButton button=null;
button=makeNavigationButton("NEW",NEW,"新建一个文件","新建");
toolBar.add(button);
button=makeNavigationButton("OPEN",OPEN,"打开一个文件","打开");
toolBar.add(button);
button=makeNavigationButton("SAVE",SAVE,"保存当前文件","保存");
toolBar.add(button);
}
private JButton makeNavigationButton(String imageName,String actionCommand,String toolTipText,String altText){
//搜索图片
String imgLocation=imageName ".png";
ImageIcon icon = new ImageIcon(imgLocation);
//初始化工具按钮
JButton button=new JButton();
//设置按钮的命令
button.setActionCommand(actionCommand);
//设置提示信息
button.setToolTipText(toolTipText);
button.addActionListener(this);
button.setIcon(icon);
return button;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().contentEquals("NEW")) {
displayResult("新建一个文件");
}else if(e.getActionCommand().contentEquals("OPEN")) {
displayResult("打开一个文件");
}else if(e.getActionCommand().contentEquals("SAVE")) {
displayResult("保存文件");
}
}
}
SwingUtils类代码如下:
public class SwingUtils {
public static void setCenter(JFrame jf) {
int screenWidth=Toolkit.getDefaultToolkit().getScreenSize().width;
int screenHeight=Toolkit.getDefaultToolkit().getScreenSize().height;
int jframeWidth = 800;
int jframeHeight = 600;
jf.setBounds((screenWidth/2)-(jframeWidth/2), (screenHeight/2)-(jframeHeight/2),
jframeWidth, jframeHeight);
}
}
以上方法由办公区教程网编辑摘抄自百度经验可供大家参考!
标签: JAVA
相关文章
IDEA中怎么设置项目更改后的分析延迟时间,IDEA是java编程语言的集成开发环境,使用该软件编写代码时,可以设置项目更改后的分析延迟时间,这样就可确保项目范围分析的正确性,具体的操作步骤如下。......
2023-07-04 594 JAVA
IDEA中怎么设置调试器最近表达式的最大数量,IDEA是java编程语言的集成开发环境,使用该软件编写代码时,可以设置调试器最近表达式的最大数量,从而有效避免调试器中表达式存储过多。......
2023-07-04 416 JAVA