下面列出了javax.swing.JButton#addMouseListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void buildButtons(JPanel tilePanel, GridBagConstraints constraints) {
JButton shuffleButton = new JButton("Shuffle");
shuffleButton.addMouseListener((MouseClickListener) event -> {
quantityPanels.forEach(it -> distribution.setQuantity(it.getTileType(), it.getQuantity()));
distribution.shuffle();
quantityPanels.forEach(it -> it.setQuantity(distribution.getQuantity(it.getTileType())));
});
JButton resetButton = new JButton("Reset");
resetButton.addMouseListener((MouseClickListener) event -> {
distribution.reset();
quantityPanels.forEach(it -> it.setQuantity(distribution.getQuantity(it.getTileType())));
});
JButton acceptButton = new JButton("Accept");
acceptButton.addMouseListener((MouseClickListener) event -> {
dispose();
quantityPanels.forEach(it -> distribution.setQuantity(it.getTileType(), it.getQuantity()));
});
constraints.gridx = 4;
tilePanel.add(shuffleButton, constraints);
constraints.gridx = 5;
tilePanel.add(resetButton, constraints);
constraints.gridx = 6;
tilePanel.add(acceptButton, constraints);
}
private void registerColorSelection(final JButton btn) {
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JDialog parent = (JDialog) SwingUtilities.getWindowAncestor(BorderPanel.this);
Color color = ExtendedColorChooser.showDialog(parent,
I18NSupport.getString("color.dialog.title"), btn.getBackground());
if (color == null) {
color = Color.BLACK;
}
btn.setBackground(color);
updateBorder();
}
});
}
public JButton createButton(final DockingActionIf action) {
JButton button = action.createButton();
button.addActionListener(this);
button.addMouseListener(this);
button.setName(action.getName());
DockingToolBarUtils.setToolTipText(button, action);
return button;
}
private Component getNamePwdPandel() {
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel jlabel = new JLabel("输入网络视频源URL");
JTextField srcText = new JTextField(30);
JButton button=new JButton("播放");
button.addMouseListener(new MouseInputAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String src=srcText.getText();
if(src!=null&&src.length()>0&&!"".equals(src.trim())) {
dialog.setVisible(false);
setVisible(true);
grabber.setUrl(src);
try {
grabber.grabBuffer();
} catch (IOException e1) {
}
}
}
});
panel.add(jlabel);
panel.add(srcText);
panel.add(button);
return panel;
}
private void initComponents() {
keboardPanel = new JPanel();
keboardPanel.setBackground(Colors.DARK);
for (int i=0; i < keys.length; i++) {
JButton btn = new JButton();
btn.setBackground(Colors.DARK);
btn.setForeground(Colors.FONT_WHITE);
btn.setText(String.valueOf(keys[i]));
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
refreshData(btn.getText());
super.mouseClicked(e);
}
});
keboardPanel.add(btn);
}
ImageIcon sendingIcon = new ImageIcon(getClass().getResource("/image/loading7.gif"));
loadingProgress.setIcon(sendingIcon);
loadingProgress.setVisible(false);
keboardPanel.add(loadingProgress, new GBC(0, 0).setFill(GBC.BOTH).setWeight(1, 1).setInsets(0, 0, 0, 0));
contactsListView = new RCListView();
}
private void buildContent() {
// create buttons:
buttonSkip = new JButton(ImageLoadingUtil.SKIP.createHighDpiImageIcon());
buttonRotateLeft = new JButton(ImageLoadingUtil.LEFT.createHighDpiImageIcon());
buttonRotateRight = new JButton(ImageLoadingUtil.RIGHT.createHighDpiImageIcon());
// set tool tips:
buttonSkip.setToolTipText("Don't place tile and skip turn");
buttonRotateLeft.setToolTipText("Rotate left");
buttonRotateRight.setToolTipText("Rotate right");
// set listeners:
buttonSkip.addMouseListener((MouseClickListener) event -> controller.requestSkip());
buttonRotateLeft.addMouseListener((MouseClickListener) event -> rotateLeft());
buttonRotateRight.addMouseListener((MouseClickListener) event -> rotateRight());
// set constraints:
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.NONE;
// add buttons:
dialogPanel.add(buttonRotateLeft, constraints);
dialogPanel.add(buttonSkip, constraints);
dialogPanel.add(buttonRotateRight, constraints);
// change constraints and add label:
constraints.fill = GridBagConstraints.VERTICAL;
constraints.gridy = 1;
constraints.gridwidth = 3;
ImageIcon defaultImage = new Tile(TileType.Null).getScaledIcon(50);
tileLabels = new ArrayList<>();
tiles = new ArrayList<>();
for (int i = 0; i < GameSettings.MAXIMAL_TILES_ON_HAND; i++) {
JLabel label = new JLabel(defaultImage);
tileLabels.add(label);
constraints.gridy++;
final int index = i;
label.addMouseListener((MouseClickListener) event -> selectTileLabel(index));
dialogPanel.add(label, constraints);
}
constraints.gridy++;
dialogPanel.add(Box.createVerticalStrut(BOTTOM_SPACE), constraints);
}
private void buildButtonClose() {
buttonClose = new JButton("Close");
buttonClose.addMouseListener((MouseClickListener) event -> {
setVisible(false);
controller.requestSkip();
});
}
public PanMode(){
setBackground(new Color(204, 153, 255));
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
Box verticalBox = Box.createVerticalBox();
add(verticalBox);
Box horizontalboxtop = Box.createHorizontalBox();
verticalBox.add(horizontalboxtop);
Component rigidArea = Box.createRigidArea(new Dimension(44, 4));
horizontalboxtop.add(rigidArea);
Box horizontalboxmid = Box.createHorizontalBox();
verticalBox.add(horizontalboxmid);
Component horizontalStrut = Box.createHorizontalStrut(4);
horizontalboxmid.add(horizontalStrut);
btnmode = new JButton("MODE FOO");
btnmode.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
GE.ge.editor_jig.toggleMode();}});
btnmode.setFont(new Font("Dialog", Font.BOLD, 12));
horizontalboxmid.add(btnmode);
Component horizontalStrut_3 = Box.createHorizontalStrut(4);
horizontalboxmid.add(horizontalStrut_3);
Box horizontalboxbottom = Box.createHorizontalBox();
verticalBox.add(horizontalboxbottom);
Component rigidArea_1 = Box.createRigidArea(new Dimension(44, 4));
horizontalboxbottom.add(rigidArea_1);}
public JButton getMenuButton() {
final JButton button = UIBox.makeDefaultButton();
button.setIcon(UIBox.getIcon(0xf0c9));
button.setToolTipText("C64 preview options.");
button.setBorder(null);
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
getOptionsMenu().show(button, e.getX(), e.getY());
}
});
return button;
}
@Override
protected Component createPreviousButton() {
AbstractButton ab = (AbstractButton) super.createPreviousButton();
JButton b = new SpinnerButton("down");
b.addActionListener((ActionListener) getUIResource(ab.getActionListeners()));
b.addMouseListener((MouseListener) getUIResource(ab.getMouseListeners()));
return b;
}
@Override
protected Component createNextButton() {
AbstractButton ab = (AbstractButton) super.createNextButton();
JButton b = new SpinnerButton("up");
b.setRequestFocusEnabled(false);
b.addActionListener((ActionListener) getUIResource(ab.getActionListeners()));
b.addMouseListener((MouseListener) getUIResource(ab.getMouseListeners()));
return b;
}
private JButton getMoreThemesButton(MouseListener aListener) {
JButton btn = new JButton(getActionIcon(MagicIcon.OPTIONS));
btn.setToolTipText(String.format("<b>%s</b><br>%s", MText.get(_S3), MText.get(_S4)));
btn.addMouseListener(aListener);
btn.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
UrlHelper.openURL(UrlHelper.URL_THEMES);
}
});
return btn;
}
private void assembleSystemMenu() {
windowMenu = new JPopupMenu();
addSystemMenuItems(windowMenu);
enableActions();
menuButton = new JButton();
menuButton.setName("InternalFrameTitlePane.menuButtonAccessibleName");
updateMenuIcon();
menuButton.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
showSystemMenu();
}
});
setInheritsPopupMenu(true);
}
private JButton getThemeFolderButton(MouseListener aListener) {
JButton btn = new JButton(getActionIcon(MagicIcon.OPEN));
btn.setToolTipText(String.format("<b>%s</b><br>%s", MText.get(_S1), MText.get(_S2)));
btn.addMouseListener(aListener);
btn.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
doOpenThemeFolder();
}
});
return btn;
}
public GUI(){
super("Easy Summarization");
menuBar = new Menu(this);
setJMenuBar(menuBar);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int espaceX=(screenSize.width-800)/2;
int espaceY=(screenSize.height-600)/2;
setBounds(espaceX, espaceY, 800, 570);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{123, 621, 0};
gridBagLayout.rowHeights = new int[]{20, 150, 150, 150, 0};
gridBagLayout.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.1, 0.3, 0.3, 0.3, Double.MIN_VALUE};
getContentPane().setLayout(gridBagLayout);
JButton b= new JButton("Summarize");
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
faire(txt.getText());
}
};
b.addMouseListener(mouseListener);
GridBagConstraints gbc_b11 = new GridBagConstraints();
gbc_b11.insets = new Insets(0, 0, 5, 5);
gbc_b11.gridx = 0;
gbc_b11.gridy = 0;
gbc_b11.fill= GridBagConstraints.BOTH;
getContentPane().add(b, gbc_b11);
panel = new ToolBar();
GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.insets = new Insets(0, 0, 5, 0);
gbc_panel.fill = GridBagConstraints.BOTH;
gbc_panel.gridx = 1;
gbc_panel.gridy = 0;
getContentPane().add(panel, gbc_panel);
//txt.setSize(20,200);
//txt.setBounds(0, 0, 10, 20);
txt.setLineWrap(true);
JScrollPane txtscrl = new JScrollPane(txt);
//txtscrl.setSize(0, 100);
///txtscrl.setPreferredSize(new java.awt.Dimension(120, 100));
//txtscrl.setPreferredSize(new java.awt.Dimension(770, 100));
GridBagConstraints gbc_b21 = new GridBagConstraints();
gbc_b21.fill = GridBagConstraints.BOTH;
gbc_b21.insets = new Insets(0, 0, 5, 0);
gbc_b21.gridwidth = 2;
gbc_b21.gridx = 0;
gbc_b21.gridy = 1;
getContentPane().add(txtscrl, gbc_b21);
//JButton b31 = new JButton("New button");
JScrollPane lsscrl = new JScrollPane(myList);
GridBagConstraints gbc_b31 = new GridBagConstraints();
gbc_b31.fill = GridBagConstraints.BOTH;
gbc_b31.insets = new Insets(0, 0, 5, 0);
gbc_b31.gridwidth = 2;
gbc_b31.gridx = 0;
gbc_b31.gridy = 2;
getContentPane().add(lsscrl, gbc_b31);
txt2.setLineWrap(true);
JScrollPane txt2scrl = new JScrollPane(txt2);
GridBagConstraints gbc_b41 = new GridBagConstraints();
gbc_b41.fill = GridBagConstraints.BOTH;
gbc_b41.gridwidth = 2;
gbc_b41.gridx = 0;
gbc_b41.gridy = 3;
getContentPane().add(txt2scrl, gbc_b41);
setVisible(true);
}
public UserActionPanel(final SwingGameController controller) {
this.controller=controller;
setMinimumSize(new Dimension(0, 114));
setOpaque(false);
final JLabel emptyLabel=new JLabel("");
imageLabel.setHorizontalAlignment(SwingConstants.CENTER);
busyItem = new ImageThrobber.Builder(BUSY_IMAGE)
.antiAlias(true)
.spinPeriod(3000)
.build();
actionButton=new JButton();
actionButton.setIcon(MagicImages.getIcon(MagicIcon.FORWARD));
actionButton.setFocusable(false);
actionButton.addActionListener(this);
actionButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
controller.passKeyPressed();
}
}
});
undoButton=new JButton(MagicImages.getIcon(MagicIcon.UNDO));
undoButton.setMargin(new Insets(1,1,1,1));
undoButton.setIconTextGap(2);
undoButton.setEnabled(false);
undoButton.setFocusable(false);
undoButton.addActionListener(this);
actionPanel=new JPanel();
actionCardLayout=new CardLayout();
actionPanel.setLayout(actionCardLayout);
actionPanel.setOpaque(false);
actionPanel.add(emptyLabel,"0");
actionPanel.add(imageLabel, "4");
actionPanel.add(busyItem,"1");
actionPanel.add(actionButton,"2");
final JPanel rightPanel=new JPanel(new GridLayout(2,1,0,2));
rightPanel.setPreferredSize(new Dimension(60,0));
rightPanel.add(actionPanel);
rightPanel.add(undoButton);
rightPanel.setOpaque(false);
contentPanel=new JPanel();
contentPanel.setLayout(new BorderLayout());
contentPanel.setOpaque(false);
final JScrollPane scroller = new JScrollPane();
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroller.getViewport().setOpaque(false);
scroller.getViewport().add(contentPanel);
setLayout(new BorderLayout());
add(scroller, BorderLayout.CENTER);
add(rightPanel,BorderLayout.EAST);
disableButton(false);
// add mouse over listeners used to display the card image
// associated with the current choice if applicable.
setCardPopupOnMouseOverListener();
setComponentOnMouseOverListener(scroller);
setComponentOnMouseOverListener(rightPanel);
setComponentOnMouseOverListener(actionButton);
setComponentOnMouseOverListener(undoButton);
}
/**
* Create the frame.
*/
public Saludo() {
super("Saludo", true, true, true, true);
setBounds(100, 100, 226, 112);
JLabel lblNombre = new JLabel("Nombre:");
txtNombre = new JTextField();
txtNombre.setColumns(10);
JButton btnSaludar = new JButton("Saludar");
btnSaludar.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
saludar();
}
});
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addComponent(lblNombre)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(txtNombre, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(53)
.addComponent(btnSaludar)))
.addContainerGap(242, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNombre)
.addComponent(txtNombre, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(btnSaludar)
.addContainerGap(206, Short.MAX_VALUE))
);
getContentPane().setLayout(groupLayout);
}
/**
* Updates the charts.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private void updateCharts() {
for (int i = 0; i < listOfChartPanels.size(); i++) {
JPanel panel = listOfChartPanels.get(i);
panel.removeAll();
JFreeChart chartOrNull = getModel().getChartOrNull(i);
if (chartOrNull != null) {
final ChartPanel chartPanel = new ChartPanel(chartOrNull) {
private static final long serialVersionUID = -6953213567063104487L;
@Override
public Dimension getPreferredSize() {
return DIMENSION_CHART_PANEL_ENLARGED;
}
};
chartPanel.setPopupMenu(null);
chartPanel.setBackground(COLOR_TRANSPARENT);
chartPanel.setOpaque(false);
chartPanel.addMouseListener(enlargeAndHoverAndPopupMouseAdapter);
panel.add(chartPanel, BorderLayout.CENTER);
JPanel openChartPanel = new JPanel(new GridBagLayout());
openChartPanel.setOpaque(false);
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
JButton openChartButton = new JButton(OPEN_CHART_ACTION);
openChartButton.setOpaque(false);
openChartButton.setContentAreaFilled(false);
openChartButton.setBorderPainted(false);
openChartButton.addMouseListener(enlargeAndHoverAndPopupMouseAdapter);
openChartButton.setHorizontalAlignment(SwingConstants.LEFT);
openChartButton.setHorizontalTextPosition(SwingConstants.LEFT);
openChartButton.setIcon(null);
Font font = openChartButton.getFont();
Map attributes = font.getAttributes();
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
openChartButton.setFont(font.deriveFont(attributes).deriveFont(10.0f));
openChartPanel.add(openChartButton, gbc);
panel.add(openChartPanel, BorderLayout.SOUTH);
}
panel.revalidate();
panel.repaint();
}
}
/**
* Updates the charts.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private void updateCharts() {
for (int i = 0; i < listOfChartPanels.size(); i++) {
JPanel panel = listOfChartPanels.get(i);
panel.removeAll();
final ChartPanel chartPanel = new ChartPanel(getModel().getChartOrNull(i)) {
private static final long serialVersionUID = -6953213567063104487L;
@Override
public Dimension getPreferredSize() {
return DIMENSION_CHART_PANEL_ENLARGED;
}
};
chartPanel.setPopupMenu(null);
chartPanel.setBackground(COLOR_TRANSPARENT);
chartPanel.setOpaque(false);
chartPanel.addMouseListener(enlargeAndHoverAndPopupMouseAdapter);
panel.add(chartPanel, BorderLayout.CENTER);
JPanel openChartPanel = new JPanel(new GridBagLayout());
openChartPanel.setOpaque(false);
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
JButton openChartButton = new JButton(OPEN_CHART_ACTION);
openChartButton.setOpaque(false);
openChartButton.setContentAreaFilled(false);
openChartButton.setBorderPainted(false);
openChartButton.addMouseListener(enlargeAndHoverAndPopupMouseAdapter);
openChartButton.setHorizontalAlignment(SwingConstants.LEFT);
openChartButton.setHorizontalTextPosition(SwingConstants.LEFT);
openChartButton.setIcon(null);
Font font = openChartButton.getFont();
Map attributes = font.getAttributes();
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
openChartButton.setFont(font.deriveFont(attributes).deriveFont(10.0f));
openChartPanel.add(openChartButton, gbc);
panel.add(openChartPanel, BorderLayout.SOUTH);
panel.revalidate();
panel.repaint();
}
}
public void addMouseListener(JButton button, ArrayList<JButton> buttons, boolean visibility) {
button.addMouseListener(new Listener(buttons, visibility));
}