新手提问 JToolBar 怎样指定工具条方向 ?JPanel 怎么传图片?初学者 希望帮写下代码 非常感谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 都做web方面的东西了 这个真忘记了 帮你顶! import javax.swing.JToolBar; import javax.swing.JButton; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JScrollPane; import javax.swing.JPanel; import java.awt.*; import java.awt.event.*; public class ToolBarDemo extends JFrame { protected JTextArea textArea; protected String newline = "\n "; public ToolBarDemo() { //Do frame stuff. super( "ToolBarDemo "); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); //Create the toolbar. JToolBar toolBar = new JToolBar(); addButtons(toolBar); //Create the text area used for output. textArea = new JTextArea(5, 30); JScrollPane scrollPane = new JScrollPane(textArea); //Lay out the content pane. JPanel contentPane = new JPanel(); contentPane.setLayout(new BorderLayout()); contentPane.setPreferredSize(new Dimension(400, 100)); contentPane.add(toolBar, BorderLayout.NORTH); contentPane.add(scrollPane, BorderLayout.CENTER); setContentPane(contentPane); } protected void addButtons(JToolBar toolBar) { JButton button = null; //first button button = new JButton(new ImageIcon( "images/left.gif ")); button.setToolTipText( "This is the left button "); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { displayResult( "Action for first button "); } }); toolBar.add(button); //second button button = new JButton(new ImageIcon( "images/middle.gif ")); button.setToolTipText( "This is the middle button "); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { displayResult( "Action for second button "); } }); toolBar.add(button); //third button button = new JButton(new ImageIcon( "images/right.gif ")); button.setToolTipText( "This is the right button "); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { displayResult( "Action for third button "); } }); toolBar.add(button); } protected void displayResult(String actionDescription) { textArea.append(actionDescription + newline); } public static void main(String[] args) { ToolBarDemo frame = new ToolBarDemo(); frame.pack(); frame.setVisible(true); } } java中文乱码问题,求解答 关于io基本概念问题,请教 关于进制转换问题 前两天被问到:JSP在J2EE中处于什么地位,请各位指教。 准备用java写一个图片处理小程序,各位有什么好建议么? 关于泛型和反射的一个问题 JTABLE的难题:我想在鼠标右键点击JTABLE时被点中的那一行被选中,如何实现? !!!java 中如何执行FTP命令?请高手指教。100分相赠 \\.任意字符匹配问题 重载的小问题 Thread对象也会在堆中分配空间吗? 关于重写的问题
!
import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.ImageIcon; import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JPanel; import java.awt.*;
import java.awt.event.*; public class ToolBarDemo extends JFrame {
protected JTextArea textArea;
protected String newline = "\n "; public ToolBarDemo() {
//Do frame stuff.
super( "ToolBarDemo ");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); //Create the toolbar.
JToolBar toolBar = new JToolBar();
addButtons(toolBar); //Create the text area used for output.
textArea = new JTextArea(5, 30);
JScrollPane scrollPane = new JScrollPane(textArea); //Lay out the content pane.
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
contentPane.setPreferredSize(new Dimension(400, 100));
contentPane.add(toolBar, BorderLayout.NORTH);
contentPane.add(scrollPane, BorderLayout.CENTER);
setContentPane(contentPane);
} protected void addButtons(JToolBar toolBar) {
JButton button = null; //first button
button = new JButton(new ImageIcon( "images/left.gif "));
button.setToolTipText( "This is the left button ");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displayResult( "Action for first button ");
}
});
toolBar.add(button); //second button
button = new JButton(new ImageIcon( "images/middle.gif "));
button.setToolTipText( "This is the middle button ");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displayResult( "Action for second button ");
}
});
toolBar.add(button); //third button
button = new JButton(new ImageIcon( "images/right.gif "));
button.setToolTipText( "This is the right button ");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displayResult( "Action for third button ");
}
});
toolBar.add(button);
} protected void displayResult(String actionDescription) {
textArea.append(actionDescription + newline);
} public static void main(String[] args) {
ToolBarDemo frame = new ToolBarDemo();
frame.pack();
frame.setVisible(true);
}
}