
正文
java组合框代码 java组合模式写法
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
JAVA代码
连连看java源代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class lianliankan implements ActionListener
{
JFrame mainFrame; //主面板
Container thisContainer;
JPanel centerPanel,southPanel,northPanel; //子面板
JButton diamondsButton[][] = new JButton[6][5];//游戏按钮数组
JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮
JLabel fractionLable=new JLabel("0"); //分数标签
JButton firstButton,secondButton; //分别记录两次被选中java组合框代码的按钮
int grid[][] = new int[8][7];//储存游戏按钮位置
static boolean pressInformation=false; //判断是否有按钮被选中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标
int i,j,k,n;//消除方法控制
public void init(){
mainFrame=new JFrame("JKJ连连看");
thisContainer = mainFrame.getContentPane();
thisContainer.setLayout(new BorderLayout());
centerPanel=new JPanel();
southPanel=new JPanel();
northPanel=new JPanel();
thisContainer.add(centerPanel,"Center");
thisContainer.add(southPanel,"South");
thisContainer.add(northPanel,"North");
centerPanel.setLayout(new GridLayout(6,5));
for(int cols = 0;cols 6;cols++){
for(int rows = 0;rows 5;rows++ ){
diamondsButton[cols][rows]=new JButton(String.valueOf(grid[cols+1][rows+1]));
diamondsButton[cols][rows].addActionListener(this);
centerPanel.add(diamondsButton[cols][rows]);
}
}
exitButton=new JButton("退出");
exitButton.addActionListener(this);
resetButton=new JButton("重列");
resetButton.addActionListener(this);
newlyButton=new JButton("再来一局");
newlyButton.addActionListener(this);
southPanel.add(exitButton);
southPanel.add(resetButton);
southPanel.add(newlyButton);
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())));
northPanel.add(fractionLable);
mainFrame.setBounds(280,100,500,450);
mainFrame.setVisible(true);
}
public void randomBuild() {
int randoms,cols,rows;
for(int twins=1;twins=15;twins++) {
randoms=(int)(Math.random()*25+1);
for(int alike=1;alike=2;alike++) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=randoms;
}
}
}
public void fraction(){
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+100));
}
public void reload() {
int save[] = new int[30];
int n=0,cols,rows;
int grid[][]= new int[8][7];
for(int i=0;i=6;i++) {
for(int j=0;j=5;j++) {
if(this.grid[i][j]!=0) {
save[n]=this.grid[i][j];
n++;
}
}
}
n=n-1;
this.grid=grid;
while(n=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=save[n];
n--;
}
mainFrame.setVisible(false);
pressInformation=false; //这里一定要将按钮点击信息归为初始
init();
for(int i = 0;i 6;i++){
for(int j = 0;j 5;j++ ){
if(grid[i+1][j+1]==0)
diamondsButton[i][j].setVisible(false);
}
}
}
public void estimateEven(int placeX,int placeY,JButton bz) {
if(pressInformation==false) {
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
pressInformation=true;
}
else {
x0=x;
y0=y;
fristMsg=secondMsg;
firstButton=secondButton;
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
if(fristMsg==secondMsg secondButton!=firstButton){
xiao();
}
}
}
public void xiao() { //相同的情况下能不能消去。仔细分析,不一条条注释
if((x0==x (y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)(y0==y))){ //判断是否相邻
remove();
}
else{
for (j=0;j7;j++ ) {
if (grid[x0][j]==0){ //判断第一个按钮同行哪个按钮为空
if (yj) { //如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边
for (i=y-1;i=j;i-- ){ //判断第二按钮左侧直到第一按钮中间有没有按钮
if (grid[x][i]!=0) {
k=0;
break;
}
else //K=1说明通过java组合框代码了第一次验证
}
if (k==1) {
linePassOne();
}
}
if (yj){ //如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边
for (i=y+1;i=j ;i++ ){ //判断第二按钮左侧直到第一按钮中间有没有按钮
if (grid[x][i]!=0){
k=0;
break;
}
else
}
if (k==1){
linePassOne();
}
}
if (y==j ) {
linePassOne();
}
}
if (k==2) {
if (x0==x) {
remove();
}
if (x0x) {
for (n=x0;n=x-1;n++ ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0 n==x-1) {
remove();
}
}
}
if (x0x) {
for (n=x0;n=x+1 ;n-- ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0 n==x+1) {
remove();
}
}
}
}
}
for (i=0;i8;i++ ) { //列
if (grid[i][y0]==0) {
if (xi) {
for (j=x-1;j=i ;j-- ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else
}
if (k==1) {
rowPassOne();
}
}
if (xi) {
for (j=x+1;j=i;j++ ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else
}
if (k==1) {
rowPassOne();
}
}
if (x==i) {
rowPassOne();
}
}
if (k==2){
if (y0==y) {
remove();
}
if (y0y) {
for (n=y0;n=y-1 ;n++ ) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0 n==y-1) {
remove();
}
}
}
if (y0y) {
for (n=y0;n=y+1 ;n--) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0 n==y+1) {
remove();
}
}
}
}
}
}
}
public void linePassOne(){
if (y0j){ //第一按钮同行空按钮在左边
for (i=y0-1;i=j ;i-- ){ //判断第一按钮同左侧空按钮之间有没按钮
if (grid[x0][i]!=0) {
k=0;
break;
}
else //K=2说明通过了第二次验证
}
}
if (y0j){ //第一按钮同行空按钮在与第二按钮之间
for (i=y0+1;i=j ;i++){
if (grid[x0][i]!=0) {
k=0;
break;
}
else
}
}
}
public void rowPassOne(){
if (x0i) {
for (j=x0-1;j=i ;j-- ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else
}
}
if (x0i) {
for (j=x0+1;j=i ;j++ ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else
}
}
}
public void remove(){
firstButton.setVisible(false);
secondButton.setVisible(false);
fraction();
pressInformation=false;
k=0;
grid[x0][y0]=0;
grid[x][y]=0;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==newlyButton){
int grid[][] = new int[8][7];
this.grid = grid;
randomBuild();
mainFrame.setVisible(false);
pressInformation=false;
init();
}
if(e.getSource()==exitButton)
System.exit(0);
if(e.getSource()==resetButton)
reload();
for(int cols = 0;cols 6;cols++){
for(int rows = 0;rows 5;rows++ ){
if(e.getSource()==diamondsButton[cols][rows])
estimateEven(cols+1,rows+1,diamondsButton[cols][rows]);
}
}
}
public static void main(String[] args) {
lianliankan llk = new lianliankan();
llk.randomBuild();
llk.init();
}
}
//old 998 lines
//new 318 lines
相关问答
Q1: 有大神说一下Java这种组合框显示图片具体的代码,谢谢了
效果图如下
swing和awt写法差别不大,我使用java组合框代码的是swing的组件
参考代码和注释如下
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//本窗口继承自JFrame 实现java组合框代码了 ActionListener接口
public class JCBFrame extends JFrame implements ItemListener{
JComboBoxString jcb;
JLabel jlimg;
// 文字
String[] ss = {"樱桃","香蕉","西瓜"};
//文字对应图片位置
String[] icons = {"img/1.jpg","img/2.jpg","img/3.jpg"};
int init = 2;//初始选择的下标--西瓜
public JCBFrame() {
jcb = new JComboBoxString(ss);
jcb.setSelectedIndex(init);//初始选择的项目
jcb.addItemListener(this);//设置条目事件响应 ,比如选中某个条目的处理
add(jcb,BorderLayout.NORTH);
jlimg = new JLabel();
jlimg.setHorizontalAlignment(JLabel.CENTER);
add(jlimg);
showImg(init);//初始显示的图片
//窗口属性的设置
setTitle("测试窗口");//标题
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(260, 300);//大小
setLocationRelativeTo(null);//居中
}
//显示图片
private void showImg(int index) {
jlimg.setIcon( new ImageIcon(this.getClass().getResource(icons[index])));
}
//main方法
public static void main(String[] args) {
new JCBFrame().setVisible(true);//实例化并可见
}
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange()==ItemEvent.SELECTED) {//当选择某个条目时
showImg(jcb.getSelectedIndex());//显示的图片改变
}
}
}
Q2: 用JAVA:我有一个JComboBox(组合框)、JLabel(装图片的)两控件。
以下程序经过java组合框代码了测试java组合框代码,供java组合框代码你参考java组合框代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyGUI extends JFrame implements ItemListener{
JComboBox cmbIndex;
JLabel lblImage;
String[] index = new String[]{"1", "2", "3"};
String[] location = new String[]{"D:\\image1.jpg", "D:\\image2.jpg", "D:\\image3.jpg"};
public MyGUI(){
super("图片选择");
cmbIndex = new JComboBox(index);
ImageIcon image = new ImageIcon(location[0]);
lblImage = new JLabel(image);
this.add(cmbIndex);
this.add(lblImage);
cmbIndex.addItemListener(this);
this.setLayout(new FlowLayout());
this.setSize(200, 200);
this.setVisible(true);
}
public void itemStateChanged(ItemEvent e) {
int index = Integer.parseInt(e.getItem().toString()) - 1;
ImageIcon image = new ImageIcon(location[index]);
lblImage.setIcon(image);
}
}
public class TestChangeImage {
public static void main(String[] args) {
MyGUI obj = new MyGUI();
}
}
Q3: java里怎么把数据库里面的某一列拿出来作为组合框的下拉项
创建一个 JComboBox 很容易看出它有哪些可用的方法,比如在 Eclipse 中打个点它就会提示方法列表。如:
JComboBox productTypes = new JComboBox();
productTypes.removeAllItems(); (这个时候打了点,略等半秒就会有方法列表)。
productTypes.addItem(new Option("001", "饮料"));
productTypes.addItem(new Option("002", "饼干"));
productTypes.addItem(new Option("003", "快餐"));
public class Option {
private final String label;
private final String value;
public Option(String label, String value) {
this.value = value;
this.label = label;
}
public String getLabel() {return this.label;}
public String getValue{) {return this.value;}
public String toString() {
return "[" + this.value + "]" + this.label;
}
}
Q4: Java 组合框的运用
问题修复后的效果图
问题出现的地方
//String sz[] = { "10,14,18" };//你用双引号把10,14,18全部括起来了,只能算1个元素了
String sz[] = { "10","14","18" };//要单独分开
一个元素本来也没啥问题,但是你后面的for循环,把数组下标移动到了1就会出现数组下标越界异常
for (int i = 0; i 3; i++) {
com.addItem(sz[i]);//如果数组SZ只有1个元素,下标是0, 如果下标是1时,就会报异常
}
Q5: 用Java编写小程序(包含组合框下拉和文本框)变换背景颜色
代码如下:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class App extends JFrame {
private JComboBoxString cbxColors;
private JTextField txtName;
private MapString, Color colorMap;
public App() {
this.setSize(300, 100);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
colorMap = new HashMap();
colorMap.put("红", Color.RED);
colorMap.put("绿", Color.GREEN);
colorMap.put("蓝", Color.BLUE);
colorMap.put("黄", Color.YELLOW);
cbxColors = new JComboBox(colorMap.keySet().toArray());
cbxColors.setPreferredSize(new Dimension(50, 23));
cbxColors.addItemListener(e - {
Color color = colorMap.get(e.getItem());
txtName.setBackground(color);
});
this.add(cbxColors);
txtName = new JTextField();
txtName.setPreferredSize(new Dimension(100, 23));
txtName.setBackground(Color.RED);
this.add(txtName);
}
public static void main(String[] args) {
new App().setVisible(true);
}
}
运行结果:
java组合框代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java组合模式写法、java组合框代码的信息别忘了在本站进行查找喔。






