首頁 > 軟體

Swing 工具列的使用

2019-12-10 01:21:05

工具列是程式中常見的一種元件,比如Word和Excel工作區上方的工具列。Java Swing也有工具列元件:JToolBar,具體怎麼使用呢?接下來小編就整理分享給大家。


1

常用的構造方法如下:構造方法1:JToolBar()建立新的工具列,預設的方向為 HORIZONTAL(水平)構造方法2:JToolBar(int orientation)建立具有指定 orientation 的新工具列構造方法3:JToolBar(String name)建立一個具有指定 name 的新工具列構造方法4:JToolBar(String name,int orientation)建立一個具有指定 name 和 orientation 的新工具列

1

常用方法如下:


1

建立JFrame容器,設定容器的名稱,容器的內容窗格:Demo27_JToolBar


2

內容窗格Demo27_JToolBar目前還是空的,接下來我們把它填滿

Demo27_JToolBar是我們自定義的一個類,它繼承JPanel,並且實現ActionListener介面



3

完成之後的效果如下:

點選工具列上的按鈕,會在下方顯示對應的內容



4

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 ma
in(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;? ? }@Overridepublic void actionPerformed(ActionEvent e) {if(e.getActionCommand().contentEquals("NEW")) {displayResult("新建一個檔案");}else if(e.getActionCommand().contentEquals("OPEN")) {displayResult("開啟一個檔案");}else if(e.getActionCommand().contentEquals("SAVE")) {displayResult("儲存檔案");}}}
=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;? ? }@Overridepublic void actionPerformed(ActionEvent e) {if(e.getActionCommand().contentEquals("NEW")) {displayResult("新建一個檔案");}else if(e.getActionCommand().contentEquals("OPEN")) {displayResult("開啟一個檔案");}else if(e.getActionCommand().contentEquals("SAVE")) {displayResult("儲存檔案");}}}

5

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)-(jframeWi
dth/2), (screenHeight/2)-(jframeHeight/2),?? ? ? ? jframeWidth, jframeHeight);}}

IT145.com E-mail:sddin#qq.com