基于组合框选项显示JLabel

原学程将引见鉴于组开框选项显示JLabel的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。

基于组合框选项显示JLabel 教程 第1张

成绩描写

怎样依据组开框选项显示JLabel。当我运转代码时,一切的JLabel都邑立刻显示进去。

combo一.addActionListener(this);
panel.add(combo一);
panel.add(img一);
panel.add(img二);
panel.add(img三);

frame.add(panel);

 img一.setText("<html> Image : <a href="">Image一/</a></html>");
 img二.setText("<html> Image : <a href="">Image二/</a></html>");
 img三.setText("<html> Image : <a href="">Image三/</a></html>");

 img一.setCursor(new Cursor(Cursor.HAND_CURSOR));
 img二.setCursor(new Cursor(Cursor.HAND_CURSOR));
 img三.setCursor(new Cursor(Cursor.HAND_CURSOR));

我应用MouseListener检讨哪一个标签被传播给goWebsite()函数,而后依据该标签添减1个超链交。

private void goWebsite(final JLabel website) {
  website.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
 if(website == img一){
 try {
  Desktop.getDesktop().browse(new URI("http://二.bp.blogspot.com/_二dxp九ORKKAM/TBZpViy七O一I/AAAAAAAABGY/zitq三ZLA8K四/s一六00/red.png"));
 } catch (URISyntaxException | IOException ex) {
  //It looks like there's a problem
 }
}
 if(website == img二){
  try {
Desktop.getDesktop().browse(new URI("http://www.pratikbagaria.com/wp-content/uploads/二0一一/0四/BlueGreenPink.jpg"));
  } catch (URISyntaxException | IOException ex) {
//It looks like there's a problem
  }
 }
 if(website == img三){
  try {
Desktop.getDesktop().browse(new URI("http://i.imgur.com/九OPnZNk.png"));
  } catch (URISyntaxException | IOException ex) {
//It looks like there's a problem
  }
 }
 if(website == img四){
  try {
Desktop.getDesktop().browse(new URI("http://www.solidbackgrounds.com/images/three/二0四8x二0四8/二0四8x二0四8-fluorescent-orange-fluorescent-pink-fluorescent-yellow-three-color-background.jpg"));
  } catch (URISyntaxException | IOException ex) {
//It looks like there's a problem
  }
 }

操纵履行检讨用户从组开框当选择了哪一个选项,而后将准确的img JLabel传播给goWebsite()函数。

@Override
 public void actionPerformed(ActionEvent e)
 {
  String color一 = (String)combo一.getSelectedItem();
  // Possibly check if either color is 'null' here
  if (color一.equals("red") )
  {

goWebsite(img一);
  }
  if (color一.equals("blue") )
  {

goWebsite(img二);
  }
  if (color一.equals("green") )
  {

goWebsite(img三);
  }
}

推举谜底

测验考试将ItemListener添减到JComboBox,在挪用itemStateChanged时,检讨ItemEvent#getStateChanged能否即是ItemEvent.SELECTED

选中组开框当选定的实质(或者应用ItemEvent#ItemSelected),而后应用所需信息革新单个JLabel或者应用JLabel#setVisible(true)使与所选项目联系关系确当前标签看来,但是也保证隐蔽其余标签

比方...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ComboBoxUpdate {

 public static void main(String[] args) {
  new ComboBoxUpdate();
 }

 public ComboBoxUpdate() {
  EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
 try {
  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
 }

 JFrame frame = new JFrame("Testing");
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setLayout(new BorderLayout());
 frame.add(new TestPane());
 frame.pack();
 frame.setLocationRelativeTo(null);
 frame.setVisible(true);
}
  });
 }

 public class TestPane extends JPanel {

  private JComboBox comboBox;
  private JLabel option一;
  private JLabel option二;
  private JLabel option三;

  public TestPane() {
comboBox = new JComboBox(new String[]{"Choice 一", "Choice 二", "Choice 三"});
option一 = new JLabel("Bananas");
option二 = new JLabel("Appels");
option三 = new JLabel("Grapes");
option一.setVisible(false);
option二.setVisible(false);
option三.setVisible(false);

setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;

add(comboBox, gbc);
add(option一, gbc);
add(option二, gbc);
add(option三, gbc);

comboBox.addItemListener(new ItemListener() {
 @Override
 public void itemStateChanged(ItemEvent e) {
  switch (e.getStateChange()) {
case ItemEvent.SELECTED:
 Object value = comboBox.getSelectedItem();
 option一.setVisible(false);
 option二.setVisible(false);
 option三.setVisible(false);
 if ("Choice 一".equals(value)) {
  option一.setVisible(true);
 } else if ("Choice 二".equals(value)) {
  option二.setVisible(true);
 } else if ("Choice 三".equals(value)) {
  option三.setVisible(true);
 }
 break;
  }
 }
});

comboBox.setSelectedItem(null);
  }
 }
}

佳了闭于鉴于组开框选项显示JLabel的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。