首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >是否有其他方法在新JFrame中显示不同的图像,具体取决于单击的JRadioButton对象,而不创建100+classes?

是否有其他方法在新JFrame中显示不同的图像,具体取决于单击的JRadioButton对象,而不创建100+classes?
EN

Stack Overflow用户
提问于 2022-06-29 02:26:03
回答 1查看 31关注 0票数 0

我创建了一个用于以下目的的Java程序:单击表示周期表中化学元素的每个JRadioButton对象时,新JFrame中由JLabel对象表示的相应屏幕截图将显示周期表中每个化学元素的各种属性。

该程序具有名为PeriodicTable、AtomicNumber和Properties的公共类。PeriodicTable包含主方法并实例化AtomicNumber。AtomicNumber是一个显示周期表的JFrame,而属性打开一个新的JFrame来显示属性的png图像。所有的化学元素都用JRadioButton对象来表示。周期表中每个化学元素的各种属性都是一个屏幕截图,它被保存为png图像,并用JLabel ImageIcon方法实例化。

在按照元素周期表的顺序排列所有化学元素之后,我注意到JFrame中没有足够的空间来添加属性的png文件。为了解决这个问题,我创建了一个名为Properties的公共类的新JFrame,其中只能显示第一个化学元素氢的png图像。虽然为属性调整png图像的大小似乎是一种可行的方法,但字体太小,因此难以辨认。为了显示属性的不同png图像,根据单击表示化学元素的JRadioButton对象,我尝试在公共类AtomicNumber中添加和删除ActionEvent中的png图像,但没有结果。

我已经考虑过创建100多个类来显示每个包含相应属性png文件的窗口的可能性,但这似乎太麻烦了。在不创建100+类的情况下,在单击表示化学元素的JRadioButton对象时,是否有其他方法显示每个相应的png属性文件?谢谢你的帮助。

我的代码如下:

代码语言:javascript
运行
AI代码解释
复制
//PeriodicTable.java
public class PeriodicTable {
  public static void main(String[] args) {
    new AtomicNumber();
  }
}
********************
//AtomicNumber.java
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.Font;
    import javax.swing.ButtonGroup;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;
    
    public class AtomicNumber extends JFrame implements ActionListener {
      private static final long serialVersionUID = 1L;
      static ButtonGroup periodicTable;
      ImageIcon[] elementImage;
      ImageIcon periodicTableIcon;
      static JButton[] button;
      static JLabel[] properties;
      static JPanel panel;
      static JRadioButton[] element;
      JTextField title;
    
  AtomicNumber() {
    elementClip = new Clip[119];
    propertiesClip = new Clip[119];
    elementAudioFile = new File[119];
    propertiesAudioFile = new File[119];
    elementAudioFile[1] = new File("H.wav");

    elementImage = new ImageIcon[119];
    properties = new JLabel[119];
    button = new JButton[2];
    button[0] = new JButton("reset");
    button[0].setBounds(1000, 500, 200, 50);
    button[1] = new JButton("restart");
    button[1].setBounds(1000, 550, 200, 50);
    for (int i = 0; i < 2; i++) {
      button[i].addActionListener(this);
      button[i].setFocusable(false);
      button[i].setFont(new Font("Times New Roman", Font.BOLD, 30));
      button[i].setPreferredSize(new Dimension(240, 50));
    }
    title = new JTextField("The Periodic Table of the Chemical Elements");
    title.setBounds(0, 0, 1080, 65);
    title.setEditable(false);
    title.setFont(new Font("Arial", Font.BOLD, 50));
    element = new JRadioButton[119];
    element[1] = new JRadioButton("1");
    element[1].setBounds(50, 75, 50, 50);
    elementImage[1] = new ImageIcon(getClass().getClassLoader().getResource("H.png"));
    properties[1] = new JLabel();
    element[2] = new JRadioButton("2");
    element[2].setBounds(925, 75, 50, 50);
    elementImage[2] = new ImageIcon(getClass().getClassLoader().getResource("He.png"));
    properties[2] = new JLabel();
    element[3] = new JRadioButton("3");
    element[3].setBounds(50, 125, 50, 50);
    elementImage[3] = new ImageIcon(getClass().getClassLoader().getResource("Li.png"));
    properties[3] = new JLabel();
    element[4] = new JRadioButton("4");
    element[4].setBounds(100, 125, 50, 50);
    elementImage[4] = new ImageIcon(getClass().getClassLoader().getResource("Be.png"));
    properties[4] = new JLabel();
    element[5] = new JRadioButton("5");
    element[5].setBounds(675, 125, 50, 50);
    elementImage[5] = new ImageIcon(getClass().getClassLoader().getResource("B.png"));
    properties[5] = new JLabel();
    element[6] = new JRadioButton("6");
    element[6].setBounds(725, 125, 50, 50);
    elementImage[6] = new ImageIcon(getClass().getClassLoader().getResource("C.png"));
    properties[6] = new JLabel();
    element[7] = new JRadioButton("7");
    element[7].setBounds(775, 125, 50, 50);
    elementImage[7] = new ImageIcon(getClass().getClassLoader().getResource("N.png"));
    properties[7] = new JLabel();
    element[8] = new JRadioButton("8");
    element[8].setBounds(825, 125, 50, 50);
    elementImage[8] = new ImageIcon(getClass().getClassLoader().getResource("O.png"));
    properties[8] = new JLabel();
    element[9] = new JRadioButton("9");
    element[9].setBounds(875, 125, 50, 50);
    elementImage[9] = new ImageIcon(getClass().getClassLoader().getResource("F.png"));
    properties[9] = new JLabel();
    element[10] = new JRadioButton("10");
    element[10].setBounds(925, 125, 50, 50);
    elementImage[10] = new ImageIcon(getClass().getClassLoader().getResource("Ne.png"));
    properties[10] = new JLabel();
    element[11] = new JRadioButton("11");
    element[11].setBounds(50, 175, 50, 50);
    elementImage[11] = new ImageIcon(getClass().getClassLoader().getResource("Na.png"));
    properties[11] = new JLabel();
    element[12] = new JRadioButton("12");
    element[12].setBounds(100, 175, 50, 50);
    elementImage[12] = new ImageIcon(getClass().getClassLoader().getResource("Mg.png"));
    properties[12] = new JLabel();
    element[13] = new JRadioButton("13");
    element[13].setBounds(675, 175, 50, 50);
    elementImage[13] = new ImageIcon(getClass().getClassLoader().getResource("Al.png"));
    properties[13] = new JLabel();
    element[14] = new JRadioButton("14");
    element[14].setBounds(725, 175, 50, 50);
    elementImage[14] = new ImageIcon(getClass().getClassLoader().getResource("Si.png"));
    properties[14] = new JLabel();
    element[15] = new JRadioButton("15");
    element[15].setBounds(775, 175, 50, 50);
    elementImage[15] = new ImageIcon(getClass().getClassLoader().getResource("P.png"));
    properties[15] = new JLabel();
    element[16] = new JRadioButton("16");
    element[16].setBounds(825, 175, 50, 50);
    elementImage[16] = new ImageIcon(getClass().getClassLoader().getResource("S.png"));
    properties[16] = new JLabel();
    element[17] = new JRadioButton("17");
    element[17].setBounds(875, 175, 50, 50);
    elementImage[17] = new ImageIcon(getClass().getClassLoader().getResource("Cl.png"));
    properties[17] = new JLabel();
    element[18] = new JRadioButton("18");
    element[18].setBounds(925, 175, 50, 50);
    elementImage[18] = new ImageIcon(getClass().getClassLoader().getResource("Ar.png"));
    properties[18] = new JLabel();
    element[19] = new JRadioButton("19");
    element[19].setBounds(50, 225, 50, 50);
    elementImage[19] = new ImageIcon(getClass().getClassLoader().getResource("K.png"));
    properties[19] = new JLabel();
    element[20] = new JRadioButton("20");
    element[20].setBounds(100, 225, 50, 50);
    elementImage[20] = new ImageIcon(getClass().getClassLoader().getResource("Ca.png"));
    properties[20] = new JLabel();
    element[21] = new JRadioButton("21");
    element[21].setBounds(150, 225, 50, 50);
    elementImage[21] = new ImageIcon(getClass().getClassLoader().getResource("Sc.png"));
    properties[21] = new JLabel();
    element[22] = new JRadioButton("22");
    element[22].setBounds(225, 225, 50, 50);
    elementImage[22] = new ImageIcon(getClass().getClassLoader().getResource("Ti.png"));
    properties[22] = new JLabel();
    element[23] = new JRadioButton("23");
    element[23].setBounds(275, 225, 50, 50);
    elementImage[23] = new ImageIcon(getClass().getClassLoader().getResource("V.png"));
    properties[23] = new JLabel();
    element[24] = new JRadioButton("24");
    element[24].setBounds(325, 225, 50, 50);
    elementImage[24] = new ImageIcon(getClass().getClassLoader().getResource("Cr.png"));
    properties[24] = new JLabel();
    element[25] = new JRadioButton("25");
    element[25].setBounds(375, 225, 50, 50);
    elementImage[25] = new ImageIcon(getClass().getClassLoader().getResource("Mn.png"));
    properties[25] = new JLabel();
    element[26] = new JRadioButton("26");
    element[26].setBounds(425, 225, 50, 50);
    elementImage[26] = new ImageIcon(getClass().getClassLoader().getResource("Fe.png"));
    properties[26] = new JLabel();
    element[27] = new JRadioButton("27");
    element[27].setBounds(475, 225, 50, 50);
    elementImage[27] = new ImageIcon(getClass().getClassLoader().getResource("Co.png"));
    properties[27] = new JLabel();
    element[28] = new JRadioButton("28");
    element[28].setBounds(525, 225, 50, 50);
    elementImage[28] = new ImageIcon(getClass().getClassLoader().getResource("Ni.png"));
    properties[28] = new JLabel();
    element[29] = new JRadioButton("29");
    element[29].setBounds(575, 225, 50, 50);
    elementImage[29] = new ImageIcon(getClass().getClassLoader().getResource("Cu.png"));
    properties[29] = new JLabel();
    element[30] = new JRadioButton("30");
    element[30].setBounds(625, 225, 50, 50);
    elementImage[30] = new ImageIcon(getClass().getClassLoader().getResource("Zn.png"));
    properties[30] = new JLabel();
    element[31] = new JRadioButton("31");
    element[31].setBounds(675, 225, 50, 50);
    elementImage[31] = new ImageIcon(getClass().getClassLoader().getResource("Ga.png"));
    properties[31] = new JLabel();
    element[32] = new JRadioButton("32");
    element[32].setBounds(725, 225, 50, 50);
    elementImage[32] = new ImageIcon(getClass().getClassLoader().getResource("Ge.png"));
    properties[32] = new JLabel();
    element[33] = new JRadioButton("33");
    element[33].setBounds(775, 225, 50, 50);
    elementImage[33] = new ImageIcon(getClass().getClassLoader().getResource("As.png"));
    properties[33] = new JLabel();
    element[34] = new JRadioButton("34");
    element[34].setBounds(825, 225, 50, 50);
    elementImage[34] = new ImageIcon(getClass().getClassLoader().getResource("Se.png"));
    properties[34] = new JLabel();
    element[35] = new JRadioButton("35");
    element[35].setBounds(875, 225, 50, 50);
    elementImage[35] = new ImageIcon(getClass().getClassLoader().getResource("Br.png"));
    properties[35] = new JLabel();
    element[36] = new JRadioButton("36");
    element[36].setBounds(925, 225, 50, 50);
    elementImage[36] = new ImageIcon(getClass().getClassLoader().getResource("Kr.png"));
    properties[36] = new JLabel();
    element[37] = new JRadioButton("37");
    element[37].setBounds(50, 275, 50, 50);
    elementImage[37] = new ImageIcon(getClass().getClassLoader().getResource("Rb.png"));
    properties[37] = new JLabel();
    element[38] = new JRadioButton("38");
    element[38].setBounds(100, 275, 50, 50);
    elementImage[38] = new ImageIcon(getClass().getClassLoader().getResource("Sr.png"));
    properties[38] = new JLabel();
    element[39] = new JRadioButton("39");
    element[39].setBounds(150, 275, 50, 50);
    elementImage[39] = new ImageIcon(getClass().getClassLoader().getResource("Y.png"));
    properties[39] = new JLabel();
    element[40] = new JRadioButton("40");
    element[40].setBounds(225, 275, 50, 50);
    elementImage[40] = new ImageIcon(getClass().getClassLoader().getResource("Zr.png"));
    properties[40] = new JLabel();
    element[41] = new JRadioButton("41");
    element[41].setBounds(275, 275, 50, 50);
    elementImage[41] = new ImageIcon(getClass().getClassLoader().getResource("Nb.png"));
    properties[41] = new JLabel();
    element[42] = new JRadioButton("42");
    element[42].setBounds(325, 275, 50, 50);
    elementImage[42] = new ImageIcon(getClass().getClassLoader().getResource("Mo.png"));
    properties[42] = new JLabel();
    element[43] = new JRadioButton("43");
    element[43].setBounds(375, 275, 50, 50);
    elementImage[43] = new ImageIcon(getClass().getClassLoader().getResource("Tc.png"));
    properties[43] = new JLabel();
    element[44] = new JRadioButton("44");
    element[44].setBounds(425, 275, 50, 50);
    elementImage[44] = new ImageIcon(getClass().getClassLoader().getResource("Ru.png"));
    properties[44] = new JLabel();
    element[45] = new JRadioButton("45");
    element[45].setBounds(475, 275, 50, 50);
    elementImage[45] = new ImageIcon(getClass().getClassLoader().getResource("Rh.png"));
    properties[45] = new JLabel();
    element[46] = new JRadioButton("46");
    element[46].setBounds(525, 275, 50, 50);
    elementImage[46] = new ImageIcon(getClass().getClassLoader().getResource("Pd.png"));
    properties[46] = new JLabel();
    element[47] = new JRadioButton("47");
    element[47].setBounds(575, 275, 50, 50);
    elementImage[47] = new ImageIcon(getClass().getClassLoader().getResource("Ag.png"));
    properties[47] = new JLabel();
    element[48] = new JRadioButton("48");
    element[48].setBounds(625, 275, 50, 50);
    elementImage[48] = new ImageIcon(getClass().getClassLoader().getResource("Cd.png"));
    properties[48] = new JLabel();
    element[49] = new JRadioButton("49");
    element[49].setBounds(675, 275, 50, 50);
    elementImage[49] = new ImageIcon(getClass().getClassLoader().getResource("In.png"));
    properties[49] = new JLabel();
    element[50] = new JRadioButton("50");
    element[50].setBounds(725, 275, 50, 50);
    elementImage[50] = new ImageIcon(getClass().getClassLoader().getResource("Sn.png"));
    properties[50] = new JLabel();
    element[51] = new JRadioButton("51");
    element[51].setBounds(775, 275, 50, 50);
    elementImage[51] = new ImageIcon(getClass().getClassLoader().getResource("Sb.png"));
    properties[51] = new JLabel();
    element[52] = new JRadioButton("52");
    element[52].setBounds(825, 275, 50, 50);
    elementImage[52] = new ImageIcon(getClass().getClassLoader().getResource("Te.png"));
    properties[52] = new JLabel();
    element[53] = new JRadioButton("53");
    element[53].setBounds(875, 275, 50, 50);
    elementImage[53] = new ImageIcon(getClass().getClassLoader().getResource("I.png"));
    properties[53] = new JLabel();
    element[54] = new JRadioButton("54");
    element[54].setBounds(925, 275, 50, 50);
    elementImage[54] = new ImageIcon(getClass().getClassLoader().getResource("Xe.png"));
    properties[54] = new JLabel();
    element[55] = new JRadioButton("55");
    element[55].setBounds(50, 325, 50, 50);
    elementImage[55] = new ImageIcon(getClass().getClassLoader().getResource("Cs.png"));
    properties[55] = new JLabel();
    element[56] = new JRadioButton("56");
    element[56].setBounds(100, 325, 50, 50);
    elementImage[56] = new ImageIcon(getClass().getClassLoader().getResource("Ba.png"));
    properties[56] = new JLabel();
    /*element[57]=new JRadioButton("57);
    elementImage[57]=new ImageIcon(getClass().getClassLoader().getResource("La.png"));
    properties[57]=new JLabel();*/

    periodicTableIcon = new ImageIcon(getClass().getClassLoader().getResource("periodicTableIcon.png"));
    periodicTable = new ButtonGroup();
    panel = new JPanel();
    panel.add(title);
    for (int i = 1; i < 57; i++) {
      element[i].addActionListener(this);
      element[i].setFocusable(false);
      element[i].setIcon(elementImage[i]);
      element[i].setHorizontalTextPosition(SwingConstants.CENTER);
      element[i].setVerticalTextPosition(SwingConstants.TOP);
      periodicTable.add(element[i]);
      panel.add(element[i]);
      panel.add(properties[i]);
    }
    for (int i = 0; i < 2; i++) {
      panel.add(button[i]);
    }
    panel.setLayout(null);
    panel.setOpaque(false);
    this.add(panel);
    this.getContentPane().setBackground(Color.WHITE);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setIconImage(periodicTableIcon.getImage());
    this.setSize(1500, 1500);
    this.setTitle("The Periodic Table of the Chemical Elements");
    this.setVisible(true);
  }
  public void actionPerformed(ActionEvent e) {
      Properties Properties = new Properties();
      for (int i = 1; i < 119; i++) {
        if (e.getSource() == button[0] && element[i].isSelected()) {
          periodicTable.clearSelection();
        }
      }
      if (e.getSource() == button[1]) {
        EventQueue.invokeLater(new Runnable() {
          public void run() {
            new AtomicNumber();
            JOptionPane.showMessageDialog(null, "Program has restarted.", "Restart", JOptionPane.INFORMATION_MESSAGE);
          }
        });
      }
      if (element[1].isSelected()) {
        new Properties().setVisible(true);}
********************
//Properties.java
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Properties extends JFrame {

  private static final long serialVersionUID = 1L;

  JLabel[] properties;
  JPanel propertiesPanel;
  Properties() {
    properties = new JLabel[119];
    properties[0] = new JLabel(new ImageIcon(getClass().getClassLoader().getResource("H_properties.png")));

    propertiesPanel = new JPanel();
    propertiesPanel.add(properties[0]);
    this.add(propertiesPanel);
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    this.setResizable(false);
    this.setSize(1250, 340);

  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-29 05:23:24

对于所有的元素,您应该只需要在类中使用。

现在,这不是一个答案,而仅仅是一个建议,也许对手头的任务采取一种不同的方法(概念),描述它的最好方法是看到它付诸行动:

这实际上来自www.britannica.com,您可以使用在这里玩。当页面显示时,单击小周期表图像,以便缩放图像。打开一个面板,显示更大版本的周期表,底部是图像旋转器。将图像旋转到最后一个图像,并使用鼠标指针单击它。窗口中将显示一个交互式周期表。现在,只需将鼠标指针移到周期表元素上,并选择要查看属性的元素。这基本上是我做这件事的方式,但是,对每个人来说都是如此。我只是想把它传过去。

元素周期表本身可以是一个由103个JLabels组成的网格类型布局,每个元素都有自己的元素映像,以及其他用于标题和图例数据的JLabels。按你喜欢的方式打扮。重要的部分是创建一个全局鼠标侦听器来处理mouseEnteredmouseExited (用于边框突出显示)和mouseClicked (用于显示属性)所有元素JLabels的事件。这甚至可能是一个内部类,例如:

代码语言:javascript
运行
AI代码解释
复制
private class ElementsMouseHandler extends java.awt.event.MouseAdapter {
    @Override
    public void mouseClicked(java.awt.event.MouseEvent evt) {
         // Do whatever you like here
    }

    @Override
    public void mouseReleased(java.awt.event.MouseEvent evt) {
         // Example:

         // Ignore the right mouse button.
         if (SwingUtilities.isRightMouseButton(evt)) {
            return;
        }
        
        /* Which JLabel fired the event. You can do this also in 
           the other events below.       */
        javax.swing.JLabel source = (javax.swing.JLabel) evt.getSource();

        // Get the name placed into the element JLabel's Name property
        String elementName = source.getName();

        /* Do whatever you want here like perhaps display another JPanel
           in the window containing the properties information or the 
           image thereof related to the elementName retrieved.    */
    }

    @Override
    public void mouseEntered(java.awt.event.MouseEvent evt) {
        // Do whatever you like here
    }

    @Override
    public void mouseExited(java.awt.event.MouseEvent evt) {
        // Do whatever you like here
    }
}

然后,当您的主要类的成员区域中,您可能有以下内容:

代码语言:javascript
运行
AI代码解释
复制
ElementsMouseHandler elementsMouseHandler;

然后,在初始化所有元素JLabels之前,您可以得到如下内容:

代码语言:javascript
运行
AI代码解释
复制
elementsMouseHandler = new ElementsMouseHandler();

当您实际初始化元素JLabel时,您可以这样做:

代码语言:javascript
运行
AI代码解释
复制
elementJLabel[idx].addMouseListener(elementMouseHandler);

现在,相同的鼠标侦听器可以处理所有元素JLabels的鼠标事件。Anyways...food代表思想。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72799802

复制
相关文章
CORS跨域
当一个资源从与该资源本身所在的服务器不同的域或端口请求一个资源时,资源会发起一个跨域 HTTP 请求。
张炳
2019/08/02
2.1K0
CORS跨域
跨域 cors
特殊说明: 解决问题的光鲜,藏着磕Bug的痛苦。 万物皆入轮回,谁也躲不掉! 以上文章,均是我实际操作,写出来的笔记资料,不会出现全文盗用别人文章!烦请各位,请勿直接盗用!
收心
2022/01/19
6040
ajax cors跨域_jquery跨域
Jsonp 的实现原理就是:创建一个回调函数,然后在远程服务上调用这个函数并且将 JSON 数据形式作为参数传递,完成回调。
全栈程序员站长
2022/09/23
2.7K0
跨域,不止CORS
我们通常提到跨域问题的时候,相信大家首先会想到的是 CORS(跨源资源共享),其实 CORS 只是众多跨域访问场景中安全策略的一种,类似的策略还有:
ConardLi
2020/11/26
1.6K0
跨域和CORS
  同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响。可以说Web是构建在同源策略基础之上的,浏览器只是针对同源策略的一种实现。    
changxin7
2019/12/19
1.1K0
cors跨域探讨
前端跨域方案很多,jsonp、iframe等等,但是个人觉得,最正宗,最无损的跨域方式还是CORS。 CORS(Cross-origin resource sharing)是一个W3C标准,翻译过来是跨域资源共享。 它允许浏览器向跨域(协议、域名、端口任一不相同)服务器发送XMLHttpRequest请求。 目前支持所有现代浏览器(>IE10) 借阅了阮一峰大神的《跨域资源共享 CORS 详解》,结合自己的理解,说一说自己的CORS的领会。
用户1394570
2018/08/08
6430
CORS跨域漏洞的学习
最近斗哥在学习CORS的漏洞和相关的一些知识梳理,网站如果存在这个漏洞就会有用户敏感数据被窃取的风险。
漏斗社区
2018/12/29
4K0
Spring的跨域CORS实现
JSONP只支持GET请求,CORS支持所有类型的HTTP请求。JSONP的优势在于支持老式浏览器,以及可以向不支持CORS的网站请求数据。
十毛
2019/10/25
5220
跨域资源CORS简介
CORS需要浏览器和服务器同时支持。目前,所有浏览器都支持该功能,IE浏览器不能低于IE10。
javascript.shop
2019/09/04
5030
Nginx 设置cors跨域
提示:有时候我们的后端是PHP文件,则需要把跨域的代码加 location ~ \.php(.*)$ 中。
ITer.996
2019/08/28
2.4K0
java配置跨域springboot配置Cors跨域
springboot配置Cors跨域、java最简单配置跨域解决方案 现在前后分离已经是很常见的一种开发方式了,所以难免会遇到跨域问题,之前用的比较多的是jsonp(本人表示没用过),之前我遇到这种问题一般都是用nginx做反向代理实现跨域请求。 不过springmvc4.2版本增加了对cors的支持,所以解决办法就更简单了,后端一个全局配置轻松解决跨域问题,比之前的都简单轻松。 cors协议不懂的可以百度哦,这里就不废话了。 由于现在大部分项目都是基于springboot做的,目前微服务的开发模式也很火,
码农笔录
2018/06/29
1.3K0
CORS 跨域与“钓鱼”
http://mpvideo.qpic.cn/0bf26qaaaaaareakmko7dnpfb5gdad2aaaaa.f10002.mp4?dis_k=b6c7c6e349458670c5b8e8f
老张的哲学
2022/04/11
2640
CORS解决跨域问题
浏览器中,网站A的网络请求访问网站A的资源(图片,HTTP请求)是很顺畅的,而想访问网站B的资源,就要面对跨域资源访问的问题了。面对跨域问题,有很多的解决方案,本文讨论使用 CORS 来解决的方案。
张云飞Vir
2020/03/27
2K0
(CORS) Nginx配置跨域
网站配置文件server 添加以下即可location / { add_header Access-Control-Allow-Origin *;add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache
用户9966982
2022/11/05
1.5K0
Flask配置Cors跨域
跨域是指:浏览器A从服务器B获取的静态资源,包括Html、Css、Js,然后在Js中通过Ajax访问C服务器的静态资源或请求。即:浏览器A从B服务器拿的资源,资源中想访问服务器C的资源。
用户1558882
2019/03/04
3.8K0
Flask配置Cors跨域
跨域问题及CORS解决跨域问题方法
跨域不一定会有跨域问题。因为跨域问题是浏览器对于ajax请求的一种安全限制:一个页面发起的ajax请求,只能是于当前页同域名的路径,这能有效的阻止跨站攻击。
Java架构师必看
2021/03/22
12.9K0
跨域共享CORS详解及Gin配置跨域
跨域简介 当两个域具有相同的协议(如http), 相同的端口(如80),相同的host,那么我们就可以认为它们是相同的域(协议,域名,端口都必须相同)。 跨域就指着协议,域名,端口不一致,出于安全考虑,跨域的资源之间是无法交互的(例如一般情况跨域的JavaScript无法交互,当然有很多解决跨域的方案) 解决跨域几种方案 /* CORS 普通跨域请求:只服务端设置Access-Control-Allow-Origin即可, 前端无须设置,若要带cookie请求:前后端都需要设置。
iginkgo18
2020/12/01
1.8K0
nginx跨域访问配置_cors跨域访问不了
跨域概念 简单来说:两个url只要协议、域名、端口有任何一个不同,都被当作是不同的域,相互访问就会有跨域问题。
全栈程序员站长
2022/11/15
4.6K0
nginx跨域访问配置_cors跨域访问不了
跨域资源共享(CORS)
简单先了解一下CORS,方便我们后续去挖一些CORS的漏洞,最近CORS也是比较火的!
黑伞安全
2019/10/16
3.6K0
AJAX 原理与 CORS 跨域
https://segmentfault.com/a/1190000011549088
Bug开发工程师
2018/09/21
1.4K0
AJAX 原理与 CORS 跨域

相似问题

闪亮,无法选择所选数据集的变量

119

跨数据集使用变量

11

声明变量集=选择

11

使用typedcolumn选择Spark数据集

23

基于变量的数据集的选择与聚焦

13
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文