我创建了一个JToolbar组件并将其添加到Frame中.工具栏使用BorderLayout.
我在工具栏中添加了三个按钮,它们显示得很好,除了我希望它们被添加到工具栏的右侧.右对齐.
然后每当我向工具栏添加其他按钮时,我希望它们添加到左侧.
我怎样才能做到这一点?
我做了以下但是会发生的事情是按钮出现在彼此的顶部:S右边的三个都在彼此之间,而左边的两个都在彼此之间..
public class Toolbar extends JToolBar {
private JToggleButton Screenshot = null;
private JToggleButton UserKeyInput = null;
private JToggleButton UserMouseInput = null;
private CardPanel cardPanel = null;
public Toolbar() {
setFloatable(false);
setRollover(true);
setLayout(new BorderLayout());
//I want to add these three to the right side of my toolbar.. Right align them :l
Screenshot = new JToggleButton(new ImageIcon());
UserKeyInput = new JToggleButton(new ImageIcon());
UserMouseInput = new JToggleButton(new ImageIcon());
cardPanel = new CardPanel();
add(Screenshot, BorderLayout.EAST);
add(UserKeyInput, BorderLayout.EAST);
add(UserMouseInput, BorderLayout.EAST); …
Run Code Online (Sandbox Code Playgroud) 我正在尝试在工具栏中添加一个图标,但最好放在哪个位置?我的桌面或应该在项目文件中创建一个新文件或添加所有图片,因为它没有显示,这是我的代码:
JToolBar toolBar = new JToolBar();
String[] iconFiles = {"pen-icon","",""};
String[] buttonLabels = {"New","Open","Save"};
icon = new ImageIcon[iconFiles.length];
Obutton = new JButton[buttonLabels.length];
for (int i = 0; i < buttonLabels.length; ++i) {
icon[i] = new ImageIcon(iconFiles[i]);
Obutton[i] = new JButton(icon[i]);
Obutton[i].setToolTipText(buttonLabels[i]);
if (i == 3)
toolBar.addSeparator();
toolBar.add(Obutton[i]);
}
Run Code Online (Sandbox Code Playgroud) 我写了以下内容,在右侧生成一个工具栏,只有它中的两个按钮一样高.我希望工具栏能够运行窗口的高度 - 从上到下.我需要做什么?
import javax.swing.*;
import java.awt.*;
public class AmtFrame extends JFrame {
private JToolBar toolBar = null;
private JButton[] buttons = new JButton[5];
private Container cp = null;
AmtFrame() {
super("AMT");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(1000, 600);
// tool bar
toolBar = new JToolBar("TaskBar", JToolBar.VERTICAL);
toolBar.add(buttons[0] = new JButton("Data Entry"));
toolBar.add(buttons[1] = new JButton("Operations"));
// layout
cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(toolBar, BorderLayout.EAST);
}
public static void main(String[] args) {
new AmtFrame().setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud) 您好我已经在我的组件的管理部分创建了一个自定义按钮链接,如下所示:
$bar = & JToolBar::getInstance('toolbar');
$bar->appendButton( 'Link', 'export', 'Export', 'index.php?option=com_component&task=export&format=raw' );
Run Code Online (Sandbox Code Playgroud)
但是没有分配图像,我不知道如何为按钮分配一个图像.有谁知道我怎么做到这一点?
我想改变JMenuBar和JToolBar的背景颜色.为此,我尝试过但没有工作.我遵循了一些网站提供的解决方案.但是,那些也没有正常工作.
这是我的代码:
import java.awt.Color;
public class JFrameDemo extends javax.swing.JFrame {
public JFrameDemo() {
Color b=new Color(0,150,255);
initComponents();
menuBar.setForeground(Color.GREEN);
toolbar.setBackground(b);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
toolbar = new javax.swing.JToolBar();
close = new javax.swing.JButton();
open = new javax.swing.JButton();
menuBar = new javax.swing.JMenuBar();
jMenu3 = new javax.swing.JMenu();
jMenu4 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
toolbar.setRollover(true);
close.setText("close");
close.setFocusable(false);
close.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
close.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
toolbar.add(close);
open.setText("open");
open.setFocusable(false);
open.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
open.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
toolbar.add(open);
menuBar.setBackground(new java.awt.Color(51, 51, 255));
jMenu3.setText("File");
menuBar.add(jMenu3);
jMenu4.setText("Edit");
menuBar.add(jMenu4);
setJMenuBar(menuBar);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); …
Run Code Online (Sandbox Code Playgroud) 我在我的文件中有这个代码
String[] iconFiles = { "state.jpg", "cursor.jpg", "arrow.jpg" };
String[] buttonLabels = { "New Node", "Attribute Change", "Add Edge"};
for (int i = 0; i < buttonLabels.length; ++i) {
icons[i] = new ImageIcon(iconFiles[i]);
buttons[i] = new JButton(icons[i]);
buttons[i].setToolTipText(buttonLabels[i]);
buttons[i].setPreferredSize(new java.awt.Dimension(648, 600));
toolBar.add(buttons[i]);
toolBar.addSeparator();
}
jp2.add(toolBar) where jp2 is a Tabbed Pane
Run Code Online (Sandbox Code Playgroud)
我有2个问题.
首先,由图标表示的图像文件不会自行调整大小,因此会被按钮大小剪切.
其次,工具栏显示在窗口中,但我可以将其拖出一个新的窗口.我怎样才能防止这种情况发生?
我不知道为什么这不起作用,但我的自定义按钮操作(任务)都没有在我的组件中执行任何操作.在我的view.html.php文件中,我有:
JToolBarHelper::custom('reports.export_csv', 'csv', '', 'CSV', false);
JToolBarHelper::custom('reports.export_mailchimp', 'mailchimp', '', 'Mailchimp', false);
Run Code Online (Sandbox Code Playgroud)
然后在我的ReportsControllerReports文件中,我有2个方法(不只是2个,还有一些其他但它们不相关),export_csv()和export_mailchimp().每当我点击按钮时,我都会收到一个JS错误,我认为这个错误阻止了操作在这些方法中执行代码.关于"b未定义"的事情.我发现我的代码和其他Joomla(核心)组件中使用的代码之间没有任何区别,所以如果有人能够对这个问题有所了解,那将非常感激(像往常一样,Joomla论坛完全没用).
我有一个简单的绘图应用程序.我需要在左侧添加一个菜单和一个工具栏.所以现在,我正在创建一个扩展JFrame的简单类,而不是使用简单的JFrame.我能够在线添加一些示例菜单,但无法弄清楚如何添加JToolBar.我尝试了几种不同的方法,但没有任何作用.我没有得到错误,一切都很顺利,但我没有看到任何JToolBar.这是我的JFrame的代码,我希望你能提供帮助.
class Menu extends JFrame {
private JMenuItem openItem;
private JMenuItem saveItem;
private JMenuItem saveAsItem;
public Menu(String title) {
openItem = new JMenuItem("Open...");
openItem.setMnemonic('O');
openItem.setAccelerator(KeyStroke.getKeyStroke("control O"));
saveItem = new JMenuItem("Save");
saveItem.setMnemonic('S');
saveItem.setAccelerator(KeyStroke.getKeyStroke("control S"));
saveAsItem = new JMenuItem("Save As...");
saveAsItem.setMnemonic('S');
saveAsItem.setAccelerator(KeyStroke.getKeyStroke("control S"));
// (2) Build menubar, menus, and add menuitems.
JMenuBar menubar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
menubar.add(fileMenu);
fileMenu.add(openItem);
fileMenu.addSeparator();
fileMenu.add(saveItem);
// (3) Add listeners to menu items
openItem.addActionListener(new OpenAction()); // TODO change
setJMenuBar(menubar);
JToolBar toolbar = …
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个工具栏,它将位于我的java swing应用程序中所有页面的顶部.
我正在创建一个JPanel,里面有一系列单独的JPanel(容器).每个JPanel(容器)都有一个北部和南部组件,或者只是一个北部组件,使用GridLayout设置.
我的问题是,我想要南北组成部分之间的小差距,但我看不出怎么做,也无法在互联网上找到任何帮助.
下面是其中一个容器的代码的工作示例:
public static void CustomersGui(){
final JFrame frame = new JFrame("Nested Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel container1 = new JPanel();
container1.setLayout(new GridLayout(2,1));
JButton buttonDiary = new JButton("Diary");
buttonDiary.setPreferredSize(new Dimension(140, 25));
JButton buttonCars = new JButton("Cars");
buttonCars.setPreferredSize(new Dimension(140, 25));
container1.add(buttonDiary, BorderLayout.NORTH);
container1.add(buttonCars, BorderLayout.SOUTH);
frame.setContentPane(container1);
frame.pack();
frame.setVisible(true);
}
Run Code Online (Sandbox Code Playgroud)