gpt4 book ai didi

java - 如何在jtoolbar中垂直添加按钮并保持按钮之间的间隙?

转载 作者:行者123 更新时间:2023-12-02 12:37:08 24 4
gpt4 key购买 nike

*每当我介绍JToolBar时,我不知道如何在jtoolbar中添加按钮 onetoolbar = new JToolBar(JToolBar.VERTICAL),它显示没有工具栏的按钮 以不规则的方式 *第二个是因为我已经尝试了很多toolbar_name.addseparator()根本不起作用,它用于维护jtoolbar中的间隙黑白按钮,所以请帮助我摆脱它!

  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);
}
}




}

}



}

最佳答案

请尝试更改以下代码。必须修改 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 来为应用程序提供良好的外观和感觉。

关于java - 如何在jtoolbar中垂直添加按钮并保持按钮之间的间隙?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45096460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com