swing问题求助 ~ 定义出跟图片一样的标签三角标志是为了排序的鼠标点击了标签 就会变成向上排序 则标签显示为name 和 上三角鼠标再点击标签 就会变成向下排序 则标签显示为name 和 下三角谢谢 各位了 ~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.awt.*;import java.awt.event.*;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JOptionPane;public class Test1 extends JFrame { private static final long serialVersionUID = 1L; private final JButton buttonUp; private final JButton buttonDown; public static void main(String[] args) { Test1 t = new Test1(); } public Test1() { setSize(200, 300); setLocation(200, 300); setLayout(new FlowLayout()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); buttonUp = new JButton("Name ↑"); buttonDown = new JButton("Name ↓"); getContentPane().add(buttonUp); getContentPane().add(buttonDown); buttonDown.setVisible(false); buttonUp.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { buttonUp.setVisible(false); buttonDown.setVisible(true); JOptionPane.showMessageDialog(null, "实现降序"); } }); buttonDown.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { buttonDown.setVisible(false); buttonUp.setVisible(true); JOptionPane.showMessageDialog(null, "实现升序"); } }); setVisible(true); }}不知道这样行不行? 用一个button就行了,两个有点浪费了. ImageIcon ico=new ImageIcon("图片地址"); ico.setImage(ico.getImage().getScaledInstance(50,20,Image.SCALE_DEFAULT)); buttonUp = new JButton(ico);小三角和文字都做成图片比较简单. package demo;import java.awt.Color;import java.awt.EventQueue;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.border.LineBorder;public class DemoForLabel extends JFrame { private static final long serialVersionUID = -1134814145773051971L; private String strUp="▲"; private String strDown="▼"; private String strName="Name"; private boolean isUp=true; /** * Launch the application * @param args */ public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { public void run() { try { DemoForLabel frame = new DemoForLabel(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public DemoForLabel() { super(); getContentPane().setLayout(null); setBounds(100, 100, 253, 188); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JLabel nameLabel = new JLabel(); nameLabel.setBorder(new LineBorder(Color.LIGHT_GRAY, 1, false)); nameLabel.addMouseListener(new MouseAdapter() { public void mouseClicked(final MouseEvent e) { isUp=!isUp; if(isUp) { nameLabel.setText(strName+" "+strUp); //下面添加实现排序的相关算法 } else { nameLabel.setText(strName+" "+strDown); //下面添加实现排序的相关算法 } } }); nameLabel.setText(strName+" "+strUp); nameLabel.setBounds(27, 28, 68, 18); getContentPane().add(nameLabel); }} 能用JDK6吗?如果可以,使用其java.swing.table.TableRowSorter可实现表格排序,省却许多麻烦。就算不可以用JDK6,也可以看看相关的源码,然后照猫画虎。以下是使用TableRowSorter的效果图: 顶,接楼上import java.awt.BorderLayout;import java.awt.EventQueue;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.RowSorter;import javax.swing.table.DefaultTableModel;import javax.swing.table.TableModel;import javax.swing.table.TableRowSorter;public class Test { public static void main(String args[]) { Runnable runner = new Runnable() { public void run() { JFrame frame = new JFrame("Sorting JTable"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Object rows[][] = { { "AMZN", "Amazon", 41.28 }, { "EBAY", "eBay", 41.57 }, { "GOOG", "Google", 388.33 }, { "MSFT", "Microsoft", 26.56 }, { "NOK", "Nokia Corp", 17.13 }, { "ORCL", "Oracle Corp.", 12.52 }, { "SUNW", "Sun Microsystems", 3.86 }, { "TWX", "Time Warner", 17.66 }, { "VOD", "Vodafone Group", 26.02 }, { "YHOO", "Yahoo!", 37.69 } }; String columns[] = { "Symbol", "Name", "Price" }; TableModel model = new DefaultTableModel(rows, columns) { public Class getColumnClass(int column) { Class returnValue; if ((column >= 0) && (column < getColumnCount())) { returnValue = getValueAt(0, column).getClass(); } else { returnValue = Object.class; } return returnValue; } }; JTable table = new JTable(model); RowSorter<TableModel> sorter = new TableRowSorter<TableModel>( model); table.setRowSorter(sorter); JScrollPane pane = new JScrollPane(table); frame.add(pane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); } }; EventQueue.invokeLater(runner); }} P2P局域网聊天软件如何自动搜索在线用户。 一个基础问题 PriorityQueue的方法求助 菜鸟提问一个简单的函数问题 一个awt swing 的弱智问题? 自己写的TAG类,在WEBLOGIC8 服务器中运行出错.是什么原因? 关于聊天室的问题???再线等待 谢谢 这样一个程序,各位大拿指点一下 请问f1book是个什么东西,jbuilder里面如何操作f1book的东西,有这样的控件吗?另外有这方面的例子吗? 请教一个关于字节流的小问题 创建String对象的问题?? 请问jdk1.6.0_10如何配置?为什么我配置好的总出问题?
import java.awt.*;
import java.awt.event.*;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;public class Test1 extends JFrame { private static final long serialVersionUID = 1L;
private final JButton buttonUp;
private final JButton buttonDown; public static void main(String[] args) {
Test1 t = new Test1();
} public Test1() { setSize(200, 300);
setLocation(200, 300);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttonUp = new JButton("Name ↑");
buttonDown = new JButton("Name ↓");
getContentPane().add(buttonUp);
getContentPane().add(buttonDown);
buttonDown.setVisible(false); buttonUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { buttonUp.setVisible(false);
buttonDown.setVisible(true);
JOptionPane.showMessageDialog(null, "实现降序"); }
});
buttonDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { buttonDown.setVisible(false);
buttonUp.setVisible(true);
JOptionPane.showMessageDialog(null, "实现升序"); }
});
setVisible(true);
}}
不知道这样行不行?
ImageIcon ico=new ImageIcon("图片地址");
ico.setImage(ico.getImage().getScaledInstance(50,20,Image.SCALE_DEFAULT));
buttonUp = new JButton(ico);小三角和文字都做成图片比较简单.
package demo;import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;public class DemoForLabel extends JFrame {
private static final long serialVersionUID = -1134814145773051971L;
private String strUp="▲";
private String strDown="▼";
private String strName="Name";
private boolean isUp=true;
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DemoForLabel frame = new DemoForLabel();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public DemoForLabel() {
super();
getContentPane().setLayout(null);
setBounds(100, 100, 253, 188);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JLabel nameLabel = new JLabel();
nameLabel.setBorder(new LineBorder(Color.LIGHT_GRAY, 1, false));
nameLabel.addMouseListener(new MouseAdapter() {
public void mouseClicked(final MouseEvent e) {
isUp=!isUp;
if(isUp)
{
nameLabel.setText(strName+" "+strUp);
//下面添加实现排序的相关算法
}
else
{
nameLabel.setText(strName+" "+strDown);
//下面添加实现排序的相关算法
}
}
});
nameLabel.setText(strName+" "+strUp);
nameLabel.setBounds(27, 28, 68, 18);
getContentPane().add(nameLabel);
}}
如果可以,使用其java.swing.table.TableRowSorter可实现表格排序,省却许多麻烦。
就算不可以用JDK6,也可以看看相关的源码,然后照猫画虎。以下是使用TableRowSorter的效果图:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;public class Test {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
JFrame frame = new JFrame("Sorting JTable");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Object rows[][] = { { "AMZN", "Amazon", 41.28 },
{ "EBAY", "eBay", 41.57 },
{ "GOOG", "Google", 388.33 },
{ "MSFT", "Microsoft", 26.56 },
{ "NOK", "Nokia Corp", 17.13 },
{ "ORCL", "Oracle Corp.", 12.52 },
{ "SUNW", "Sun Microsystems", 3.86 },
{ "TWX", "Time Warner", 17.66 },
{ "VOD", "Vodafone Group", 26.02 },
{ "YHOO", "Yahoo!", 37.69 } };
String columns[] = { "Symbol", "Name", "Price" };
TableModel model = new DefaultTableModel(rows, columns) {
public Class getColumnClass(int column) {
Class returnValue;
if ((column >= 0) && (column < getColumnCount())) {
returnValue = getValueAt(0, column).getClass();
} else {
returnValue = Object.class;
}
return returnValue;
}
}; JTable table = new JTable(model);
RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(
model);
table.setRowSorter(sorter);
JScrollPane pane = new JScrollPane(table);
frame.add(pane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}