jtoolbar在java中的意思_java – 为什么addSeparator()不能使用我的JToolBar?

我无法让JSeparator出现在JToolBar中.我的工具栏创建如下:

public class ToolBar extends JToolBar {

super();

FlowLayout layout = new FlowLayout(FlowLayout.LEFT, 10, 5);

setLayout(layout);

add(new JButton("Button 1"));

addSeparator();

add(new JButton("Button 2"));

add(new JButton("Button 3"));

addSeparator();

// Show

setVisible(true);

setFloatable(false);

}

任何想法都会非常感激,我一直试图让它工作太长时间>(

解决方法:

在那里尝试代码,当我调用addSeparator()方法时,它会在按钮之间创建一个空格,但没有可见的分隔线.

但是,如果我将方法更改为addSeparator(new Dimension(20,20)),则会创建可见的分隔线.

问题可能是默认的外观会创建一个高度为1的分隔符,因此您将无法看到它.

我在Mac OSX上运行它.

标签:java,layout,swing,jtoolbar

来源: https://codeday.me/bug/20190827/1740124.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的Java记事本实现,并添加注释说明。下面是实现代码: ```java import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; public class SimpleNotepad extends JFrame implements ActionListener { private JTextArea textArea = new JTextArea(); // 文本区域 public SimpleNotepad() { super("SimpleNotepad"); setJMenuBar(createMenuBar()); // 创建菜单栏 getContentPane().add(createToolBar(), BorderLayout.NORTH); // 创建工具栏 getContentPane().add(new JScrollPane(textArea), BorderLayout.CENTER); // 添加滚动条 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(800, 600); setLocationRelativeTo(null); setVisible(true); } private JMenuBar createMenuBar() { JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("文件"); JMenuItem newMenuItem = new JMenuItem("新建"); JMenuItem openMenuItem = new JMenuItem("打开"); JMenuItem saveMenuItem = new JMenuItem("保存"); JMenuItem exitMenuItem = new JMenuItem("退出"); newMenuItem.addActionListener(this); openMenuItem.addActionListener(this); saveMenuItem.addActionListener(this); exitMenuItem.addActionListener(this); fileMenu.add(newMenuItem); fileMenu.add(openMenuItem); fileMenu.add(saveMenuItem); fileMenu.addSeparator(); fileMenu.add(exitMenuItem); menuBar.add(fileMenu); return menuBar; } private JToolBar createToolBar() { JToolBar toolBar = new JToolBar(); JButton newButton = new JButton(new ImageIcon("new.gif")); JButton openButton = new JButton(new ImageIcon("open.gif")); JButton saveButton = new JButton(new ImageIcon("save.gif")); newButton.addActionListener(this); openButton.addActionListener(this); saveButton.addActionListener(this); toolBar.add(newButton); toolBar.add(openButton); toolBar.add(saveButton); return toolBar; } public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if (command.equals("新建")) { textArea.setText(""); } else if (command.equals("打开")) { openFile(); } else if (command.equals("保存")) { saveFile(); } else if (command.equals("退出")) { System.exit(0); } } private void openFile() { JFileChooser fileChooser = new JFileChooser(); int result = fileChooser.showOpenDialog(this); if (result == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try { BufferedReader reader = new BufferedReader(new FileReader(file)); String line = null; while ((line = reader.readLine()) != null) { textArea.append(line + "\n"); // 读取文件并添加到文本区域 } } catch (IOException e) { e.printStackTrace(); } } } private void saveFile() { JFileChooser fileChooser = new JFileChooser(); int result = fileChooser.showSaveDialog(this); if (result == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try { BufferedWriter writer = new BufferedWriter(new FileWriter(file)); writer.write(textArea.getText()); // 将文本区域的内容写入文件 writer.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { new SimpleNotepad(); } } ``` 这个简单的记事本实现了基本的新建、打开、保存和退出功能。它使用了JMenuBar、JToolBar和JTextArea等Swing组件,可以在Windows、Linux和Mac OS X上运行。希望这个实现代码和注释能够帮助到您。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值