[Javaのスイング] JavaでJToolBarは
JToolBarは、通常のメニューの下に配置され、対応するボタンを含むアイコンが長いバーです, Wordのツールバーのような, 新しいボタンのExcel, オープン, セーブ, …

私たちは、上の画像のようなプログラムを実施します, 含めて 3 新規ボタン, オープン, ツールバー上のアイコンをクリックする. あなたはJTextAreaに下のボタンをクリックすると、対応するイベントが表示されます.
package nguyenvanquan7826.JToolBar;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
/**
* --------------------- @author nguyenvanquan7826 ---------------------
* ------------------ website: cachhoc.net -------------------
*/
public class DemoJToolBar extends JFrame implements ActionListener {
public static final String NEW = "new";
public static final String OPEN = "open";
public static final String SAVE = "save";
public static String iconNew = "iconNew.png";
public static String iconOpen = "iconOpen.png";
public static String iconSave = "iconSave.png";
private JTextArea ta;
public DemoJToolBar() {
setJMenuBar(createMenuBar());
addContent();
setDisplay();
}
/**
* set display for JFrame with title is "Demo JToolBar"
*/
private void setDisplay() {
setTitle("Demo JToolBar");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
/**
* demo menu
*/
private JMenuBar createMenuBar() {
JMenuBar mb = new JMenuBar();
mb.add(new JMenu("File"));
mb.add(new JMenu("Help"));
return mb;
}
/**
* add content of JFrame, it contain a JToolBar and a JTextArea to display
* event on toolbar
*/
private void addContent() {
setLayout(new BorderLayout());
add(createToolBar(), BorderLayout.PAGE_START);
add(new JScrollPane(ta = createTextArea(15, 30)), BorderLayout.CENTER);
}
/**
* create a toolbar
*/
private JToolBar createToolBar() {
JToolBar tb = new JToolBar();
tb.add(createButton(iconNew, "New", NEW, "Create a new file"));
tb.add(createButton(iconOpen, "Open", OPEN, "Open a file"));
tb.add(createButton(iconSave, "Save", SAVE, "Save this file"));
return tb;
}
/**
* create a JTextArea to display events
*/
private JTextArea createTextArea(int row, int col) {
JTextArea ta = new JTextArea(row, col);
ta.setWrapStyleWord(true);
ta.setLineWrap(true);
// ta.setEditable(false);
return ta;
}
/**
* create a Button, it has a icon, tex (if not found icon), actionCommand
* and tool tip text
*/
private JButton createButton(String iconLink, String text, String command,
String toolTip) {
JButton btn = new JButton();
btn.setActionCommand(command);
btn.setToolTipText(toolTip);
ImageIcon icon = createIcon(iconLink);
if (icon == null) {
btn.setText(text);
} else {
btn.setIcon(icon);
}
btn.setActionCommand(command);
btn.addActionListener(this);
return btn;
}
/**
* create icon
*/
private ImageIcon createIcon(String iconLink) {
URL imageURL = JToolBarOracle.class.getResource(iconLink);
if (imageURL != null) {
return new ImageIcon(imageURL);
} else {
System.out.println("image " + iconLink + " not found");
return null;
}
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
String action = "null";
if (command.equals(NEW)) {
action = "new file";
} else if (command.equals(OPEN)) {
action = "open file";
} else if (command.equals(SAVE)) {
action = "save file";
}
ta.append(action + "\n");
}
public static void main(String[] args) {
new DemoJToolBar();
}
}
このツールバーには、トップバー上にマウスを置くことができます (複数のドットを持つセクション) 抗力は、水平または垂直位置に置く. あなたは固定したい場合は、メソッドを使用して toolBar.setFloatable(偽);, さらに、そのようなJTextFieldのような他のオブジェクトを追加することができます,… 検索バーがそのような作る.
アイコン 新しい, オープン, セーブ 以下にあなたをpakage:

続きを読む: クラスJToolBarは, ユーザーJToolBarは, Javaのスイング



この記事をオフラインでいただきありがとうございます. 私はJToolBarのいくつかの場所を満たしているこの部分について学んでいます, あなたの指示はその後おけた
あなたの助けのために何かを投稿して嬉しい! 定期的にオフラインでのブログを訪問!