*每当我介绍JToolBar时,我都不知道如何在jtoolbar中添加按钮 onetoolbar = new JToolBar(JToolBar.VERTICAL),它显示没有工具栏的按钮 以不规则的方式 *第二个是因为我已经尝试了很多toolbar_name.addseparator()根本没有用于维护jtoolbar中的间隙黑白按钮,所以plz帮我摆脱它!
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.awt.event.*;
import javax.swing.JToolBar;
import java.awt.Container;
public class paint implements ActionListener
{
JFrame oneframe;
JPanel onepanel,twopanel,threepanel,fourpanel,fivepanel,sixpanel;
JTabbedPane onepane;
Font onefont,twofont;
JButton fileButton,viewButton,homeButton,newButton,openButton;
JButton saveButton,printButton,exitButton;
JButton redButton,yellowButton,greenButton;
JButton ovalButton,rectangleButton,circleButton;
JButton cutButton,copyButton,pasteButton;
JTable dataTable,staffTable,onedataTable,onestaffTable,complaintTable;
int colorcheck,tollcheck,x,y,x1,y1;
BorderLayout bl;
public paint()
{
oneframe=new JFrame("Paint");
onepanel=new JPanel();
twopanel=new JPanel();
//twopanel=(JPanel)getContentPane();
threepanel=new JPanel();
fourpanel=new JPanel();
fivepanel=new JPanel();
sixpanel=new JPanel();
onepanel.setLayout(null);
fileButton=new JButton("File");
homeButton=new JButton("Home");
viewButton=new JButton("View");
newButton=new JButton("New");
openButton=new JButton("Open");
saveButton=new JButton("Save");
printButton=new JButton("Print");
exitButton=new JButton("Exit");
JToolBar onetoolbar = new JToolBar();
onetoolbar.add(newButton);
onetoolbar.add(openButton);
onetoolbar.add(saveButton);
onetoolbar.add(printButton);
onetoolbar.add(exitButton);
//toolbar.setRollover(true);
//onetoolbar.setMargin(new Insets(10,10,10,10));
onetoolbar.setFloatable(false);
onetoolbar.addSeparator();
onetoolbar.setPreferredSize(new Dimension(1500,30));
onetoolbar.setBounds(0,0,1500,30);
//twopanel.add(toolbar, BorderLayout.WEST);
onepanel.add(onetoolbar);
oneframe.add(onepanel);
//twopanel.setLayout(null);
//bl=new BorderLayout();
twopanel.setLayout(null);
cutButton=new JButton("Cut");
//cutButton.setBounds(5,5,70,25);
copyButton=new JButton("Copy");
//copyButton.setBounds(80,5,70,25);
pasteButton=new JButton("Paste");
//pasteButton.setBounds(40,35,70,25);
JToolBar toolbar = new JToolBar(JToolBar.VERTICAL);
toolbar.add(cutButton);
toolbar.add(copyButton);
toolbar.add(pasteButton);
//toolbar.setRollover(true);
toolbar.setFloatable(false);
toolbar.setPreferredSize(new Dimension(200, 400));
toolbar.setBounds(0,0,200,400);
//twopanel.add(toolbar, BorderLayout.WEST);
twopanel.add(toolbar);
oneframe.add(twopanel);
Font font=new Font("PLAYBILL",Font.BOLD,32);
//headingLabel.setFont(font);
//headingLabel.setForeground(Color.BLUE);
onepane=new JTabbedPane();
onepane.setBackground(Color.GREEN);
Font tfont=new Font("Cntury Gothic",Font.BOLD,15);
onepane.setFont(tfont);
newButton.addActionListener(this);
openButton.addActionListener(this);
saveButton.addActionListener(this);
printButton.addActionListener(this);
exitButton.addActionListener(this);
onepane.addTab("File",onepanel); //adding buttons in tab
onepane.addTab("Home", twopanel);
onepane.addTab("View",threepanel);
oneframe.add(onepane);
oneframe.setSize(1500,1500);
// oneframe.setResizable(false);
oneframe.setVisible(true);
}
public static void main(String[] args)
{
new paint();
}
public void actionPerformed(ActionEvent evt)
{
if(evt.getSource().equals(newButton))
{
JOptionPane.showConfirmDialog(paint.this.oneframe,
"Do you want to Save?");
}
else if(evt.getSource().equals(openButton))
{
JFileChooser chooser=new JFileChooser();
int result=chooser.showOpenDialog(oneframe);
if(result==JFileChooser.APPROVE_OPTION)
{
System.out.println("File Opened");
}
}
else if(evt.getSource().equals(saveButton))
{
JFileChooser chooser=new JFileChooser();
int result=chooser.showSaveDialog(oneframe);
if(result==JFileChooser.APPROVE_OPTION)
{
System.out.println("File Saved");
}
}
/*else if(evt.getSource().equals(printButton))
{
/*JFileChooser chooser=new JFileChooser();
int result=chooser.showSaveDialog(oneframe);
} */
else if(evt.getSource().equals(printButton))
{
JOptionPane.showConfirmDialog(paint.this.oneframe, "Do you want to print?");
}
else if(evt.getSource().equals(exitButton))
{
int dialogButton = JOptionPane.YES_NO_CANCEL_OPTION;
JOptionPane.showConfirmDialog (null, "Do you want to save Existing data?","Do you want to save?",dialogButton);
{
if(dialogButton == JOptionPane.YES_OPTION)
{
JFileChooser chooser=new JFileChooser();
int result=chooser.showSaveDialog(oneframe);
if(result==JFileChooser.APPROVE_OPTION)
{
System.out.println("File Saved");
}
}
else
{
System.exit(0);
}
}
}
}
}
答案 0 :(得分:0)
请尝试下面的代码更改。必须修改setBounds()
方法的参数才能正确显示垂直按钮。此外addSeparator()
方法无效,因此在下面建议每个按钮后添加分隔符的解决方法。
JToolBar onetoolbar = new JToolBar(JToolBar.VERTICAL);
onetoolbar.add(newButton);
onetoolbar.add(new JSeparator());
onetoolbar.add(openButton);
onetoolbar.add(new JSeparator());
onetoolbar.add(saveButton);
onetoolbar.add(new JSeparator());
onetoolbar.add(printButton);
onetoolbar.add(new JSeparator());
onetoolbar.add(exitButton);
onetoolbar.setFloatable(false);
onetoolbar.setPreferredSize(new Dimension(1500, 30));
onetoolbar.setBounds(0, 0, 100, 200);
我建议使用JMenuBar
代替JToolBar
来为应用程序提供漂亮的外观和感觉。