我正在创建一个类似于gedit的文本编辑器。在工具栏中,我使用了10个带图标的按钮。我正在使用JFrame
和BorderLayout
。工具栏位于北部。但问题是,按钮变得太大,工具栏几乎覆盖了一半的框架。所有的图标大小为512x512px。我尝试调整图标大小,但它没有奏效。如果图像的图标被删除,它会正常工作。跨越JFrame的一半的JToolBar
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
class RSP extends WindowAdapter implements ActionListener, CaretListener
{
private JFrame f;
private JMenuBar mb;
private JMenu m1, m2, m3, m4, m5, m6, m7, m16;
private JMenuItem m11, m12, m13, m14, m15, m17, m18, m21, m22, m23, m24, m25, m26, m27, m28, m31, m32, m33, m41, m42, m51, m52, m53, m61, m62, m71, m72, m161, m162, m163;
private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10;
private JToolBar tb;
private JTabbedPane tp;
private JLabel l;
private JTextArea[] ta;
private int caretPos, lineNum, columnNum;
public RSP()
{
f=new JFrame("RSPEditor");
mb=new JMenuBar();
CreateMenuBar();
tb=new JToolBar();
tb.setFloatable(false);
CreateToolbar();
tp=new JTabbedPane(JTabbedPane.TOP,JTabbedPane.SCROLL_TAB_LAYOUT);
l=new JLabel("",JLabel.TRAILING);
ta=new JTextArea[10];
f.setSize(800,550);
f.setVisible(true);
f.setJMenuBar(mb);
f.add(tb,BorderLayout.NORTH);
f.add(tp,BorderLayout.CENTER);
f.add(l,BorderLayout.SOUTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.addWindowListener(this);
}
public void CreateToolbar()
{
tb.setLayout(new GridLayout(1,10,5,5));
tb.setSize(f.getWidth(),20);
b1=new JButton("New",new ImageIcon(getClass().getResource("new.png")));
b2=new JButton("Open",new ImageIcon(getClass().getResource("open.png")));
b3=new JButton("Save",new ImageIcon(getClass().getResource("save.png")));
b4=new JButton("Undo",new ImageIcon(getClass().getResource("undo.png")));
b5=new JButton("Redo",new ImageIcon(getClass().getResource("redo.png")));
b6=new JButton("Cut",new ImageIcon(getClass().getResource("cut.png")));
b7=new JButton("Copy",new ImageIcon(getClass().getResource("copy.png")));
b8=new JButton("Paste",new ImageIcon(getClass().getResource("paste.png")));
b9=new JButton("Find",new ImageIcon(getClass().getResource("find.png")));
b10=new JButton("Replace",new ImageIcon(getClass().getResource("replace.png")));
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
tb.add(b1);
tb.add(b2);
tb.add(b3);
tb.add(b4);
tb.add(b5);
tb.add(b6);
tb.add(b7);
tb.add(b8);
tb.add(b9);
tb.add(b10);
}
public void CreateMenuBar()
{
m1=new JMenu("File");
m2=new JMenu("Edit");
m3=new JMenu("Search");
m4=new JMenu("Execute");
m5=new JMenu("Tools");
m6=new JMenu("Documents");
m7=new JMenu("Help");
mb.add(m1);
mb.add(m2);
mb.add(m3);
mb.add(m4);
mb.add(m5);
mb.add(m6);
mb.add(m7);
m11=new JMenuItem("New");
m12=new JMenuItem("Open");
m13=new JMenuItem("Save");
m14=new JMenuItem("Save As");
m15=new JMenuItem("Print");
m16=new JMenu("Recent Files");
m161=new JMenuItem("A");
m162=new JMenuItem("B");
m163=new JMenuItem("C");
m16.add(m161);
m16.add(m162);
m16.add(m163);
m17=new JMenuItem("Close");
m18=new JMenuItem("Quit");
m21=new JMenuItem("Undo");
m22=new JMenuItem("Redo");
m23=new JMenuItem("Cut");
m24=new JMenuItem("Cop[y");
m25=new JMenuItem("Paste");
m26=new JMenuItem("Delete");
m27=new JMenuItem("Select All");
m28=new JMenuItem("Insert Date and Time");
m31=new JMenuItem("Find");
m32=new JMenuItem("Replace");
m33=new JMenuItem("Goto Line");
m41=new JMenuItem("Compile");
m42=new JMenuItem("Run");
m51=new JMenuItem("Indent");
m52=new JMenuItem("Spell Check");
m53=new JMenuItem("Full Screen");
m61=new JMenuItem("Save All");
m62=new JMenuItem("Close All");
m71=new JMenuItem("How To");
m72=new JMenuItem("About");
m1.add(m11);
m1.add(m12);
m1.addSeparator();
m1.add(m13);
m1.add(m14);
m1.addSeparator();
m1.add(m15);
m1.addSeparator();
m1.add(m16);
m1.addSeparator();
m1.add(m17);
m1.add(m18);
m2.add(m21);
m2.add(m22);
m2.addSeparator();
m2.add(m23);
m2.add(m24);
m2.add(m25);
m2.add(m26);
m2.addSeparator();
m2.add(m27);
m2.addSeparator();
m2.add(m28);
m3.add(m31);
m3.add(m32);
m3.addSeparator();
m3.add(m33);
m4.add(m41);
m4.add(m42);
m5.add(m51);
m5.add(m52);
m5.addSeparator();
m5.add(m53);
m6.add(m61);
m6.add(m62);
m7.add(m71);
m7.add(m72);
m11.addActionListener(this);
m11.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
m12.addActionListener(this);
m12.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
m13.addActionListener(this);
m13.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
m14.addActionListener(this);
m14.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK | ActionEvent.SHIFT_MASK));
m15.addActionListener(this);
m15.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK));
m161.addActionListener(this);
m162.addActionListener(this);
m163.addActionListener(this);
m17.addActionListener(this);
m17.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK));
m18.addActionListener(this);
m18.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK));
m21.addActionListener(this);
m21.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
m22.addActionListener(this);
m23.addActionListener(this);
m23.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
m24.addActionListener(this);
m24.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
m25.addActionListener(this);
m25.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
m26.addActionListener(this);
m27.addActionListener(this);
m27.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
m28.addActionListener(this);
m31.addActionListener(this);
m31.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, ActionEvent.CTRL_MASK));
m32.addActionListener(this);
m32.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, ActionEvent.CTRL_MASK));
m33.addActionListener(this);
m33.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, ActionEvent.CTRL_MASK));
m41.addActionListener(this);
m42.addActionListener(this);
m42.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F9, ActionEvent.CTRL_MASK));
m51.addActionListener(this);
m52.addActionListener(this);
m53.addActionListener(this);
m61.addActionListener(this);
m62.addActionListener(this);
m71.addActionListener(this);
m72.addActionListener(this);
}
public void createNewTab()
{
ta[tp.getTabCount()]=new JTextArea();
ta[tp.getTabCount()].addCaretListener(this);
ta[tp.getTabCount()].setFont(new Font("Ariel",Font.PLAIN,20));
tp.addTab("New File",new JScrollPane(ta[tp.getTabCount()]));
}
public void windowClosing(WindowEvent e) //prompt for save
{
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(b1) || e.getSource().equals(m11))
createNewTab();
else if(e.getSource().equals(b2) || e.getSource().equals(m12));
//fileOpen();
else if(e.getSource().equals(b3) || e.getSource().equals(m13));
//save;
else if(e.getSource().equals(m14));
//saveas
else if(e.getSource().equals(m15));
//print
else if(e.getSource().equals(m161));
//recent1
else if(e.getSource().equals(m162));
//recent2
else if(e.getSource().equals(m163));
//recent3
else if(e.getSource().equals(m17));
//close
else if(e.getSource().equals(m18));
//quit
else if(e.getSource().equals(b4) || e.getSource().equals(m21));
//undo
else if(e.getSource().equals(b5) || e.getSource().equals(m22));
//redo
else if(e.getSource().equals(b6) || e.getSource().equals(m23));
//cut
else if(e.getSource().equals(b7) || e.getSource().equals(m24));
//copy
else if(e.getSource().equals(b8) || e.getSource().equals(m25));
//paste
else if(e.getSource().equals(m16));
//delete
else if(e.getSource().equals(m17));
//select all
else if(e.getSource().equals(m18));
//insert d&t
else if(e.getSource().equals(b9) || e.getSource().equals(m31));
//find
else if(e.getSource().equals(b10) || e.getSource().equals(m32));
//replace
else if(e.getSource().equals(m33));
//goto
else if(e.getSource().equals(m41));
//compile
else if(e.getSource().equals(m42));
//run
else if(e.getSource().equals(m51));
//indent
else if(e.getSource().equals(m52));
//spell chk
else if(e.getSource().equals(m53));
//fullscreen
else if(e.getSource().equals(m61));
//saveall
else if(e.getSource().equals(m62));
//closeall
else if(e.getSource().equals(m71));
//howto
else if(e.getSource().equals(m72));
//about
}
public void caretUpdate(CaretEvent e)
{
JTextArea editArea=(JTextArea)e.getSource();
lineNum=1;
columnNum=1;
try
{
caretPos=editArea.getCaretPosition();
lineNum=editArea.getLineOfOffset(caretPos);
columnNum=caretPos-editArea.getLineStartOffset(lineNum);
lineNum++;
l.setText("L "+lineNum+", C "+columnNum+" ");;
}
catch(Exception ex) { }
}
public static void main(String args[])
{
new RSP();
}
}
你有什么,所以任何适当的大小远吗? – Anptk 2014-11-08 09:42:34
我有一个菜单栏,一个作为状态栏的标签在南方,jtabbedpane在中心。 tabbedpane有jscrollpane作为每个选项卡,其中包含一个jtextarea。 – SanketB 2014-11-08 09:44:30
发布您的代码以获得更好的解决方案 – Anptk 2014-11-08 09:44:55